pointer confusion

This is a discussion on pointer confusion within the C Programming forums, part of the General Programming Boards category; why does the following code crash?? char *a="string a"; char *b="string b"; *b=*a;...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    pointer confusion

    why does the following code crash??

    char *a="string a";
    char *b="string b";

    *b=*a;

  2. #2
    Unregistered
    Guest
    Probably others can expand on my answer. You are defining two separate and different character arrays and then saying that one is equal to the other. By convention, the compiler is made to reject this.

  3. #3
    Unregistered
    Guest
    The name of an array is equal to the address of the first element. These arrays have different addresses, so they can't be equated. Use strcpy.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    This is my theory. It crashes because the strings you are using are consts and are probably stored in read only memory. When you do *b=*a you are simply trying to copy the first character of array a, to the first character of array b. If a is pointing to read only memory, the prog will core.

    Here's some code to backup this. example 1 is your code that will crash, example 2 uses malloc to obtain memory for the second array (and therefore it won't be in read only memory).

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *a="abcd"; 
    	char *b="efgh";
    	
    	printf("Variable a/Char 1 is %c\n", *a);
    	printf("Variable b/Char 1 is %c\n", *b);
    	printf("Variable a as a string is %s\n", a);
    	printf("Variable b as a string is %s\n", b);
    	
    	*b=*a;
    	
    	printf ("After assignment...\n");
    	printf("Variable a/Char 1 is %c\n", *a);
    	printf("Variable b/Char 1 is %c\n", *b);
    	printf("Variable a as a string is %s\n", a);
    	printf("Variable b as a string is %s\n", b);
    	
    	return(0);
    	
    }
    /* Program output
    Variable a/Char 1 is a
    Variable b/Char 1 is e
    Variable a as a string is abcd
    Variable b as a string is efgh
    Segmentation fault (core dumped)
    */
    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *a="abcd"; 
    	char *b=malloc(10);
    	strcpy (b, "efgh");
    	
    	printf("Variable a/Char 1 is %c\n", *a);
    	printf("Variable b/Char 1 is %c\n", *b);
    	printf("Variable a as a string is %s\n", a);
    	printf("Variable b as a string is %s\n", b);
    	
    	*b=*a;
    	
    	printf ("After assignment...\n");
    	printf("Variable a/Char 1 is %c\n", *a);
    	printf("Variable b/Char 1 is %c\n", *b);
    	printf("Variable a as a string is %s\n", a);
    	printf("Variable b as a string is %s\n", b);
    	
    	return(0);
    	
    }
    /* Program output
    Variable a/Char 1 is a
    Variable b/Char 1 is e
    Variable a as a string is abcd
    Variable b as a string is efgh
    After assignment...
    Variable a/Char 1 is a
    Variable b/Char 1 is a
    Variable a as a string is abcd
    Variable b as a string is afgh
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    1
    what hammer says is right , here you have two strings which are constants , that is they are written in a region which is read only , so in your code , an attempt is made to alter the strings , which is not allowed and will be flagged as an error

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Okay, but what about char a[]="abcdf"

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Monster
    Okay, but what about char a[]="abcdf"
    In the examples, a is the source, and is therefore allowed to be in read only memory.

    I expect you meant to ask about char b[]="efgh" ?
    In this case, you have an array, not a pointer. The declaration has a different meaning. In short:

    >char *b="efgh"
    is a pointer to a const array of chars.

    >char b[]="efgh"
    is an array of chars, containing a default string. The array itself will be in read/write memory.

    You can read about this in the C FAQ from comp.lang.c.
    Here's a link to it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer confusion
    By rohit_second in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 04:25 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM
  5. Pointer confusion...
    By fkheng in forum C Programming
    Replies: 13
    Last Post: 06-23-2003, 10:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21