Thread: why does this pointer definition crashes...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    why does this pointer definition crashes...

    Code:
    main()
    {
    
        char const *p = "hello";
        char *anp ="google";
    
    	//p = anp;
    
        printf("value of p:%s\n",*p);        Line K
        printf("value of anp:%u\n",anp);
        printf("value of p:%u\n",p);
    
    	return 0;
    
    }
    Why does this program crashes?
    Commenting the Line K work fine?
    Is this an seg fault?

    Thanks in advance

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %s prints a string. You are dereferencing the pointer, giving you a constant character, not a string. You should have: %c if you want just one character, or %s and just the name of p, not *p.

    You really should be listening to your compiler. It will tell you what you're doing wrong most of the time.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Novice
    Join Date
    Jul 2009
    Posts
    568
    %s specifier expects a pointer to the first element of a null-terminated array, i.e. string, which a string literal (e.g. "hello") or array name happen to be equivalent to. In this case, you're feeding it 'h' which is 0x68 hex, a memory address you're program is unlikely to have access to. Hence the segfault.

    (This is to the best of my understanding. Please correct me if I'm totally off.)

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks all for your time..........

    From this arises another problem:

    Code:
    void mycpy(char *d,char *s);
    
    int main()
    {
        char *dest,*src ;//= "google";
        *src = "google" ;
        mycpy(dest,src);
    	printf("content of dest:%s\n",dest);
    	printf("content of src:%s\n",src);
    	return 0;
    }
    
    void mycpy(char *d,char *s)
    {
    
    	while((*d++ = *s++),*s!='\0')  line L
    	//while((*d++ = *s++))                       line J
            *d = '\0';                                      // add null char at the last
    	printf("In mycpy content of dest:%s\n",d);
    	printf("In mycpy content of src:%s\n",s);
    }
    Another crash at line L........same with line J uncommented
    Looks like i need a more understanding on this.....

    What may be the reason for this crash?
    Last edited by sanddune008; 04-28-2010 at 03:52 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    dest has no memory allocated for it, and isn't actually set to point any place specific, so you're just writing over top of some random spot in memory.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Multiple problems.
    Using pointers without allocating memory: SourceForge.net: Common mistakes and errors - cpwiki
    Trying to assign a string to a char, dereferencing said pointer in the process.

    Quote Originally Posted by quzah View Post
    %s prints a string. You are dereferencing the pointer, giving you a constant character, not a string. You should have: %c if you want just one character, or %s and just the name of p, not *p.

    You really should be listening to your compiler. It will tell you what you're doing wrong most of the time.


    Quzah.
    Out of the two popular compilers, GCC and Visual C++, only GCC complains about this.
    Problems like these are not picked up by most compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by quzah View Post
    dest has no memory allocated for it, and isn't actually set to point any place specific, so you're just writing over top of some random spot in memory.


    Quzah.
    then how do i go abt this without using dynamic memory allocation.......
    Last edited by sanddune008; 04-28-2010 at 04:04 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sanddune008 View Post
    then how do i go abt this without using dynamic memory allocation.......
    If you need to use a pointer, you don't. If you can use an array, you would do
    Code:
    char dest[SOME_LARGE_NUMBER];

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Thanks all....and for the document...


    Code:
    void mycpy(char *d,char *s)
    {
    	while
    		
    		
    		
    		((*d++ = *s++));
    }

    how come following while declaration still remains valid?
    How will the lexical parser interpret the following source file?
    As the complete statement is across the 4 line?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sanddune008 View Post
    Thanks all....and for the document...


    Code:
    void mycpy(char *d,char *s)
    {
    	while
    		
    		
    		
    		((*d++ = *s++));
    }

    how come following while declaration still remains valid?
    How will the lexical parser interpret the following source file?
    As the complete statement is across the 4 line?
    As C tokenizes a source file, different types of whitespace are not considered significant (a space versus a newline versus a tab all work the same). So the fact that there are three lines between "while" and "(" is just the same to the compiler as if there was just one space between.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    Can you
    
    
    
                still read this?
    So can your compiler!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    I have yet another code that crashes:
    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    typedef struct node 
    {
    	int val;
    	struct node *next;
     
    }sNodeDetails;
    
    sNodeDetails *head;
    
    
    void vAdd(int valu);
    void vDisplay(sNodeDetails *NDetails);
    
    int main()
    {
        
    	head = (sNodeDetails*)(0);
        vAdd(1);
    	vAdd(2);
    	vAdd(3);
    	vAdd(4);
    	vAdd(5);
    
    	vDisplay(head);
    	//free(head);
    	return 0;
    }
    
    
    void vAdd(int valu)
    {
    	sNodeDetails *temp,*NodeDetails;
        temp = head;
    
    	NodeDetails = (sNodeDetails*)(malloc(sizeof(sNodeDetails)));  Problem Line
    	//if(NodeDetails)
    		//printf("no memory:\n");
    
        
    	NodeDetails->val = valu;
    
    
        if(head == NULL)
    	{
    		head = NodeDetails;
    		(*head).next = NULL;
    	
    	}
    	else
    	{
    		while((*temp).next == NULL)
            {
    			temp->next = NodeDetails;
    			temp = (*head).next;			
    			break;
    		}
    
    	}
    }
    
    void vDisplay(sNodeDetails *NDetails)
    {
    	while(NDetails)
    	{
    	
    		printf("Display val:\n");
    		printf("%d\n",(*NDetails).val);
    		NDetails = NDetails->next;
    	}
    
    }
    Looks like In Problem line:

    Twice malloc is assiging memory from the heap........but further it doesn't

    What may be the cause?


    Thanks in advance

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    I was at fault......dumb mistake

    Code:
    void vAdd(int valu)
    {
    	sNodeDetails *temp,*NodeDetails;
        temp = head;
    
    	NodeDetails = (sNodeDetails*)(malloc(sizeof(sNodeDetails)));
    	if(NodeDetails == 0)
    		printf("no memory:\n");
    
        
    	NodeDetails->val = valu;
    
    
        if(head == NULL)
    	{
    		head = NodeDetails;
    		(*head).next = NULL;
    	
    	}
    	else
    	{
    		while((*temp).next != NULL)
    			temp = (*temp).next; 
    		if((*temp).next == NULL)
            {
    			temp->next = NodeDetails;
    			temp->next->next = NULL;
    		
    		}
    
    	}
    }
    Last edited by sanddune008; 04-29-2010 at 01:28 AM.

  14. #14
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Hi,

    I am trying to do a string comparison

    Code:
    int iStrCmp(const char *str1,const char *str2)
    {
    	while(*str1++ == *str2++,(str1!='\0'));
    	return (*str1 - *str2);
    
    }
    I am unable to determine why the following function fails?

    Okay following line is at fault

    while(*str1++ == *str2++,(str1!='\0'));


    Replace it with


    while(*str1++ == *str2++,(*str1!='\0'));


    Thanks in advance
    Last edited by sanddune008; 05-02-2010 at 04:50 PM.

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you using comma operator for a reason? Do you know what that reason is?

    str1 is a pointer and cannot/should not be compared to a character.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM