0

Very simple question but within my code I have two char* variables.

char* port = "1100"; char* ip = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr); 

The first is a port number and the second tells the ip address of a given interface.

If I wanted to create a new variable say, char* both, why is it that I cannot say:

char* both = ip + port; 

with the output of 172.21.8.179 1100? How can I get a new variable with that output? Thanks

4
  • 1
    cplusplus.com/reference/cstring/strcat Commented Aug 19, 2015 at 22:58
  • 1
    Do not add tags for different languages! C++ is not C. Commented Aug 19, 2015 at 22:59
  • 2
    And read what a pointer is. You seem not even to have an idea about them. I'd recommend to read a C bokk or do a tutorial. Commented Aug 19, 2015 at 23:00
  • 1
    Because C doesn't have string types. You have to do that yourself, as well as doing the memory management. Commented Aug 19, 2015 at 23:09

3 Answers 3

1

you probably want to use snprintf

char buff[100]; snprintf(buff, sizeof(buff), "%s %s", port, ip); 
Sign up to request clarification or add additional context in comments.

5 Comments

check your snprintf arguments
Taking a sledgehammer to crack a nut. What's wrong with strcpy here?
@Olaf You'd need a strcpy followed by two strcat to accomplish what the snprintf is doing. And using a sledgehammer to crack nuts will improve your arm strength, so it's a win-win :)
@user3386109: Yes, that is the attitude leading to GUIs with 16GiB requirement to run a text editor. Alone the format string parser is more bloat than the 10 strcpys. Also: it is just two memcpy and 1 strlen. And a modern compiler wil either inline them and use highly optimized code. If you are a brain-worker, there is nothing gained being a body-builder.
I expect that the OP doesnt want to simply add two stirng together. I suspect he is composing a string like "https://%s:%d", in that case snprintf is what he wants
0

You can't add two strings in C because, well, they're not actually strings. They're just pointers. And adding two pointers results in a pointer that points at the address that is the sum of the two original addresses.

To concatenate two char*s together, you can use the strcat(char * destination, const char * source) function. Just make sure that your both pointer is pointing to enough memory to actually hold the concatenated string!

9 Comments

"And adding two pointers results in a pointer that points at the address that is the sum of the two original addresses." - it's a constraint violation. Pointers aren't fancy integers
strings are not pointers. They behave like pointers in some circumstances, but they are not pointers.
@Peter I think what he's trying to say that strings in C are implemented as an array of characters, and arrays are often managed using pointers to their first element. Therefore, you can't use + when concatenating strings because adding two pointers is meaningless. The wording can be tricky.
@M.M Just cast (int) before them and their addresses would be added.
@KiraSama The cast changes a pointer value to an integer value. Integers can be added, pointers can't. Also, your cast may cause undefined behaviour on a system with 64-bit pointers and 32-bit ints.
|
0

You can use sprintf() function call

...

sprintf(char * buffer, const char * format, ...)

Dynamic

char* res = (char*)malloc(15); char* str1 = "Hello "; char* str2 = "World!"; sprintf(res, "%s%s", str1, str2); puts(res); // Hello World! 

Static

char res[15]; char str1[] = "Hello "; char str2[] = "World!"; sprintf(res, "%s%s", str1, str2); puts(res); // Hello World! 

you're able to add integers into a C string too with the %d format specifier.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.