Pointer Initialization

This is a discussion on Pointer Initialization within the C Programming forums, part of the General Programming Boards category; I was having a lecture about pointers and I thought I understood already. Until I tried this simple code that ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    9

    Pointer Initialization

    I was having a lecture about pointers and I thought I understood already. Until I tried this simple code that doesn't work. It gives "Segmentation fault: 11".
    Code:
    #include<stdio.h>
    
    int main()
    {
     int *a;
        scanf("%d", a);
        printf("%d", *a);
     return 0;
    }
    This is my understanding: in the initialization int *a, I set a to be an address pointing to an integer. Then I scan an integer and store it on the address of a. Finally I print the integer stored in the address of a. So, what went wrong?

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Quote Originally Posted by johan.g1 View Post
    I was having a lecture about pointers and I thought I understood already. Until I tried this simple code that doesn't work. It gives "Segmentation fault: 11".
    Code:
    #include<stdio.h>
    
    int main()
    {
     int *a;
        scanf("%d", a);
        printf("%d", *a);
     return 0;
    }
    This is my understanding: in the initialization int *a, I set a to be an address pointing to an integer. Then I scan an integer and store it on the address of a. Finally I print the integer stored in the address of a. So, what went wrong?
    'a' is a pointer, but it doesn't point to anywhere yet

    Give it somewhere to point to first
    Code:
    #include<stdio.h>
    
    
    int main()
    {
        int b;
        int *a;
    
        a = &b;
    
    
        scanf("%d", a);
        printf("%d", *a);
        return 0;
    }
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12

    Post

    A better example would be the following:

    By better I am not saying better than Click_here's example, I am referring to the original poster's example.

    Code:
    #include <stdio.h>
    
    int main(){
        int a = 0;
        int * b = &a;
    
        *b = 5;
        printf("%d", a);
    
        return 0;
    }
    This uses pointers to change the variable 'a' to 5 without directly using it.
    Last edited by Khaltazar; 11-14-2012 at 07:57 PM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Quote Originally Posted by Khaltazar View Post
    A better example would be the following:
    This uses pointers to change the variable 'a' to 5 without directly using it.
    Aside from a and b being renamed, Click_here's didn't directly use the pointee either. The pointee being the thing that a pointer points to, I mean.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Location
    Louisville, KY
    Posts
    12

    Post

    Quote Originally Posted by whiteflags View Post
    Aside from a and b being renamed, Click_here's didn't directly use the pointee either. The pointee being the thing that a pointer points to, I mean.
    I didn't mean it was better than the poster above me, I was replying to the original poster. I usually don't use quotes unless I am replying to someone other than the original poster.

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    My code was not an example - It was just a slight change to the OP's code to make it work.
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    Thanks. I think I got it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct pointer initialization
    By pe4enka in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2010, 04:16 AM
  2. Pointer Initialization Within A Struct
    By SMurf in forum C Programming
    Replies: 15
    Last Post: 02-09-2009, 10:27 AM
  3. A problem with pointer initialization
    By zyklon in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 11:42 AM
  4. Initialization from incompatible pointer type
    By Jailan in forum C Programming
    Replies: 9
    Last Post: 10-28-2007, 09:17 PM
  5. initialization of double pointer with new
    By GaPe in forum C++ Programming
    Replies: 8
    Last Post: 01-19-2005, 10:09 AM

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