Another mock question that doesnt compile!
Hi, another mock exam question right here! Ive tried to run this code to work out what it is doing but it says line1 hasnt been declared. The exam question asks what the output will be so i will have to try and work out what its result is meant to be.
Code:
char c *line, line2[20]
int index = 0;
strcpy(line2, "2 and 2 are 4");
c = line2[0];
line1 = (char *)malloc(15*sizeof(char));
while (c != '\0'
{
line1[index] = c;
if (c = '2') printf("ok\n");
index = index+1;
c = line2[index];
};
line1[index-1]='\0';
printf("%s\n",line1);
There are no scanf so that i know there is no input. The two outputs I can see if "ok \n" and printing the line1 string. There is a while conditional saying that while the value of c is not equal to "\0" then print ok. I have two questions about the code thus far.
1. What does '\0' mean. It says if the value of c is not equal to that. However it looks like c is being defined as a char so how can a char be 0?
2. What else is going on wiht this code, i dont really undestand the malloc and strcpy lines.
3. Is the [20] after line2 defining how many is that maximum chars that this can hold in the string?