3

Please help me understand what is the difference using an initialized character array like char line[80]="1:2" ( which doesn't work !! ) and using char line[80] followed by strcpy(line,"1:2").
As per my understanding in first case I have a charachter array, it has been allocated memory, and i am copying a string literal to it. And the same is done in the second case. But obviously I am wrong. So what is wrong with my understanding.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void tokenize(char* line)
{
   char* cmd = strtok(line,":");

    while (cmd != NULL)
    {
    printf ("%s\n",cmd);
    cmd = strtok(NULL, ":");
    } 
}

int main(){
char line[80]; //char line[80]="1:2" won't work 
/*
char *line;
line = malloc(80*sizeof(char));
strcpy(line,"1:2");
*/
strcpy(line,"1:2");
tokenize(line);
return 0;
}
3
  • As it is char array, value should be like {'1',':','2'}; during initialization. At one index only one character value will be plcaed. Commented Dec 3, 2014 at 5:27
  • char line[80]="1:2" is string literal and you can't modified it in tokenize function( using strtok) So it not work. Commented Dec 3, 2014 at 5:57
  • char line[80]="1:2" --> char line[80]="1:2"; Commented Dec 3, 2014 at 11:13

1 Answer 1

3

You are wrong. The result of these two code snippets

char line[80] = "1:2";

and

char line[80];
strcpy( line, "1:2" );

is the same. That is this statement

char line[80] = "1:2";

does work.:) There is only one difference. When the array is initialized by the string literal all elements of the array that will not have a corresponding initialization character of the string literal will be initialized by '\0' that is they will be zero-initialized. While when function strcpy is used then the elements of the array that were not overwritten by characters of the string literal will have undeterminated values.

The array is initialized by the string literal (except the zero initialization of its "tail") the same way as if there was applied function strcpy.

The exact equivalence of the result exists between these statements

char line[80] = "1:2";

and

char line[80];
strncpy( line, "1:2", sizeof( line ) );

that is when you use function strncpy and specify the size of the target array.

If you mean that you passed on to function tokenize directly the string literal as an argument of the function then the program has undefined behaviour because you may not change string literals.

According to the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

Sign up to request clarification or add additional context in comments.

4 Comments

I think OP might be say that tokenize(line); function not work by using string literal.And of course it will not work using string literal because it's constant string and illegal to modify it.
strncpy() stops copying when either: 1) the source string, current char, is '\0' 2) the size parameter is reached. The copy will stop when the first (of either) condition is met. so the two methods of initializing the array are not equivalent
@user3629249 You are wrong. Read the description of strncpy.
Hi user3629249 Yeah I guess got what you are trying to say. Its like you can have arr[80]="1:2:3:\0:4:5". As VladfromMoscow said in this case arr will contain all the 6 characters followed by a tail of \0 and if I use strcpy(arr,"1:2:3:\0:4:5") as @you pointed out, I will only be copying "1:2:3:\0" and then all rest of 76 elements will have unditermined values So I believe VladfromMosco is also correct but you also discussed the corner case, clarifying when strcpy would fail to have similar behavior. The answer from VladfromMosco though seems correct to me as it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.