CrazyEngineers
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Vishal Sharma

    MemberJul 26, 2015

    radha gogia
    [HASHTAG]#include[/HASHTAG]<stdlib.h>
    [HASHTAG]#include[/HASHTAG]<string.h>
    int main(void)
    {
    char *p ;
    strcat(p, "abc");
    printf("\n %s \n", p);
    return 0;
    }

    If I print this the value at some random location pointed to by pointer , I get some garbage value so then there must be some compile-time error rather than segmentation fault ,plz clarify this .
    This might be a brain fart but you missed stdio header (you are using printf).

    Coming to the question.
    You have not allocated any space for *p and you are trying to store something. hence, the segmentation fault. However, for this program, even if you allocate some space, you'll see some crap at the start followed by the string you are concatenating. Program with allocated space. Execute it, and check me on the output.

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main(void)
    {
    char *p = (char *)malloc(1024);
    strcat(p, "abc");
    printf("\n %s \n", p);
    return 0;
    }
    
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register