#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Pointers: very basic question


    Hey there,
    I'm new to C and I just got started in pointers. In a given tutorial I get "segmentation fault: 11" when reproducing this simple code:


    #include <stdio.h>
    int main(){
    int *n;
    *n = 20;
    printf("%i\n", *n);
    return 0;
    }

    Why do I get "segmentation fault: 11" when I was supposed to get 20 as output?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    1) use code tags. Surround your code with [ code ] and [ /code ] (without the spaces)

    2) you have a pointer: n. It is not initialized: you cannot safely put things where it points to (it doesn't point anywhere relevant) or look where it points.

    Try this
    Code:
    #include <stdio.h>
    
    int main(void) {
        int pointee = 42;
        int *n;
        n = &pointee; /* make n point to the pointee */
        printf("%d\n", pointee);
        printf("%d\n", *n);
        *n = 20;
        printf("%d\n", pointee);
        printf("%d\n", *n);
        return 0;
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0
    Thanks, your code worked fine. I tried doing something different and this time it worked.

    Code:
    #include <stdio.h>
    
    int main(){
    
    	// printing a pointer
    	
    	int *p;
    	*p = 100;
    	
    	int n = 200;
    	
    	printf("Address [%p] = [%d]\n", p, *p);
    	printf("Address [%p] = [%d]\n", &n, n);  
    	
    	return 0;
    }

    When I go back to my first example and changed to this it still gave me the segmentation fault:

    Code:
    #include <stdio.h>
    
    int main(){
    
    	/*
    	POINTERS
    	*/
    	
    	// *a (sets variable a as a pointer)
    	// &b (returns the memory address of variable b)
    
    	int *n;
    	*n = 290;
    	
    	// printing the memory address of *n
    	printf("Memory address: %p\n", n);
    	printf("Value: %i", *n);
    	
    	/*
    	// this will print 20
    	printf("%i\n", *n);
    	*/
    	
    	return 0;
    }
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    Originally Posted by Alcorel
    I tried doing something different and this time it worked.
    You got unlucky. It shouldn't have worked to make you notice the error: the pointer still points nowhere valid --- it hasn't been initialized or assigned a valid value.

    You might like to read the comp.lang.c FAQ, particularly sections 4 and 5.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    As bdb told you, you just got very unlucky, because instead of crashing your program just clobbered a memory location instead, causing whatever good data that had been stored there to be replaced with garbage, thus corrupting your data. This corruption can very well give you very unexpected results that you are completely unable to understand how to correct. And when you do this in a larger project, it will be virtually impossible to troubleshoot and correct.

    Never use an uninitialized variable, especially not a pointer!

    Do you think that pointers are just some magical abstract idea? They are firmly rooted in the silicon lattices of your computer's hardware! Every single memory location that you store any data in resides in a memory chip. Locations within memory chips, and hence within your computer's memory space, are organized as sequential words, each of which has a unique address -- in most all personal computers, the size of those words is eight bits, a byte. Data larger than one byte uses multiple consecutive bytes. When your program is compiled, every variable is given a memory location whose address is associated with that variable, such that whenever your program accesses a variable's data for either reading or writing, it does so by using the address associated with that variable.

    Pointers are simply indirect addressing which you learned in assembly class. Instead of loading a variable with data, you load it with the memory address where that data is located. The key phrase here is: "you load it with the memory address". You have to tell your program where that memory location is, which you do by initializing the pointer. If you don't, then it could just point anywhere and using it will result either in a segfault or in clobbered data.

    Why don't you read your own comments?
    Code:
    	// *a (sets variable a as a pointer)
    	// &b (returns the memory address of variable b)
    
    	int *n;
    	*n = 290;
    To implement what the comments say:
    Code:
        int  b;  // b is a variable; it has a memory location assigned to it
        int *a;   // a is a pointer; right now, it contains a garbage address and is unusable
        a = &b;    // a now contains the address of b and is now useable
        *a = 290;   //  now this is a safe operation, since a points to somewhere useable
    In police, SWAT, and military training and even just when a civilian spends time on a firing range, one thing that is constantly emphasized is muzzle discipline. Always know where your firearm is pointing and that it is not pointing towards a friendly. The same applies to using pointers in C: always know where your pointers are pointing. Pointer discipline is absolutely essential. Otherwise you end up shooting yourself in the foot.

    Comments on this post

    • bdb agrees : I loved the "muzzle disciplin" metaphor
    • MrFujin agrees : great explanation and I now learned the origin of "shooting your foot" :)

IMN logo majestic logo threadwatch logo seochat tools logo