Вопрос

I'm trying first to generate a random number and cast it to a string. It does it properly but the compiler gives "warning: cast to pointer from integer of different size". And then, the main issue here is that I'm trying to concatenate strings to build a response, but for some reason not even the first strcat works and it just halts there.

char *name, *cseq;
srand (time(NULL));
char *session = (char*) ( rand() % (9999 - 1000 + 1) + 1000 );

char* response = "RTSP/1.0 200 OK\nCSeq: ";                     
strcat( response, cseq );                   
strcat ( response, "\n" );                      
strcat ( response, "Session " );
strcat ( response, session );                       
strcat ( response, "\n\n" );

printf("Response: %s", response);
Это было полезно?

Решение

No, it doesn't do it "properly"; your code is very broken. Compilers are not trying to be funny when they emit warnings, they generally know what they're speaking about and it's you who need to figure out what is wrong with your code that causes it to trigger a warning. Because there should be none.

In your case, this code for instance:

char *session = (char*) ( rand() % (9999 - 1000 + 1) + 1000 );

is not valid, and using session as if it points at a valid string will most likely invoke undefined behavior.

You cannot "cast" things into strings in C, unless they're 0-terminated arrays of bytes describing characters in the expected encoding to begin with.

You need to create the string from the random number, using e.g. sprintf() or something like it:

const int session_id = rand() % (9999 - 1000 + 1) + 1000;
char session[32];

snprintf(session, sizeof session, "%d", session_id);

Of course, once you start using snprintf() you might just as well use it for all the formatting, and drop strcat() which is a pretty sharp-edged tool anyway.

Also, as pointed out by simonc, you can't modify a string like you're doing it. You should probably have something like:

char response[512];

snprintf(response, sizeof response, "RTSP/1.0 200 OK\nCSeq: %s\nSession %d\n\n",
         cseq, session_id);
printf("Response: '%s'", response);

Note that this uses session_id from above, but does all the formatting in one call to snprintf(), so no need for the temporary string version of the session ID.

Of course, if you want to return this out of the function, you need to think about buffer management, you can't return response as-is since it's a local variable that will go out of scope when the function exits.

Другие советы

char* response = "RTSP/1.0 200 OK\nCSeq: "; 

gives you a read-only string literal. You can't modify the contents of this.

A quick and dirty fix would be to change response to be writeable with a hard-coded length:

char response[200] = "RTSP/1.0 200 OK\nCSeq: "; 

A better approach might be to calculate the lengths of the various string components then malloc memory for response. If you do this, be sure to call free later.

In addition to the problems with session that unwind has pointed out, note that cseq is uninitialised in your example so dereferencing it will crash.

With the following

char *session = (char*) ( rand() % (9999 - 1000 + 1) + 1000 );

you're interpreting the random number as a memory address, which is legal but probably not what you wanted to do. The result is that you dump random memory into the response, obtaining if you're lucky only garbage, and at worst segmentation faults. If you want to convert it to a string (e.g. 1234 -> "1234") you should use something like

char session[6];
snprintf(session,6, "%d", rand() % (9999 - 1000 + 1) + 1000 );

(see also previous discussion here).

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top