1
int main(){
    char *c=(char*)malloc(4*sizeof(char));
    *c='a';
    c++;
    *c='b';
    c++;
    *c='c';
    c++;
    *c='\0';
    printf("%s",c);
    return 0;
}

With this code I can print every single character (e.g. printf("%c",*(--c)); ), but when I try to print the whole string using printf("%s",c); the program prints nothing! Why is this happening ?

5
  • 5
    You have increased c to the last character '\0'... So nothing to print. Commented Aug 25, 2016 at 9:56
  • 1
    When you printf("%s",c); c points to \0 Commented Aug 25, 2016 at 9:56
  • @Jarod42 Thanks,got it! Commented Aug 25, 2016 at 9:58
  • @DimChtz Thanks,got it! Commented Aug 25, 2016 at 9:58
  • That really was an error you should have been able to detect yourself (yes, that includes beginners). Next time please use a debugger, single step and watch the variables before asking. Commented Aug 25, 2016 at 11:38

4 Answers 4

6

The pointer has been moved. So it is now pointing to the last character which is a "\0". So, nothing is printed. Preserve the pointer in another variable and try to print using that. It will then print correctly.

Try like below:

int main()
{
    char *p;
    char *c=(char*)malloc(4*sizeof(char));
    p = c;
    *c='a';
    c++;
    *c='b';
    c++;
    *c='c';
    c++;
    *c='\0';
    printf("%s",p);
    free(p);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

You have increased c to the last character '\0'... So nothing to print.

You may change your code to:

int main(){
    char *p = (char*)malloc(4 * sizeof(char));
    char* c = p;
    *c = 'a';
    c++;
    *c = 'b';
    c++;
    *c = 'c';
    c++;
    *c = '\0';
    printf("%s", p);
    free(p);
    return 0;
}

or

int main(){
    char *c = (char*)malloc(4 * sizeof(char));

    c[0] = 'a';
    c[1] = 'b';
    c[2] = 'c';
    c[3] = '\0';
    printf("%s", c);
    free(c);
    return 0;
}

Comments

1

c is a pointer and therefore the expression c++; changes the pointer location. After calling c++ 3 times you end up with c pointing to \0. To fix this issue create a temporary pointer to iterate through the elements. Also you program has a memory leak. Do not forget to free the memory!

Comments

1

Because your string is now pointed to last character in allocated string. Try something like

int main(){
    char *c=(char*)malloc(4*sizeof(char));
    char* temp = c;
    *c='a';
    c++;
    *c='b';
    c++;
    *c='c';
    c++;
    *c='\0';
    printf("%s",c); // as a result of ++ operator, your pointer is no longer point at the beginning of string
    printf("%s,temp"); // << This will print whole string as it still pointed to the first address of the string
    free(temp);
    return 0;
}

1 Comment

Where is the memory leak?

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.