Thread: Arrays with Pointers

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    10

    Arrays with Pointers

    hi =)
    i have tried to save array values with pointers but i got "segmentation fault".
    how can i do it corectly?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void main()
    {
        
        int *pdizi[3]={NULL};
        int i;
    
        for(i=0; i<3; i++)
        scanf("%d",pdizi[i]);
    
        for(i=0; i<3; i++)
        printf("%d\t%p\n",*pdizi[i],pdizi[i]);
    
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    for(i=0; i<3; i++)
    scanf("%d",pdizi[i]);
    This code loads garbage, and pointers are assigned some unspecified values.
    Later you try to dereference this garbage (dereferece invalid pointers), hence the error.

    Probably a simple non-pointer array would suffice

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    10
    Code:
    int dizi[3];
        int *pdizi[3]={NULL,NULL,NULL};
        int i;
    
        for(i=0; i<3; i++)
        dizi[i]=i*i;
    
        for(i=0; i<3; i++)
        pdizi[i]=&dizi[i];
    
        for(i=0; i<3; i++)
        printf("%d\t%p\n",*pdizi[i],pdizi[i]);
    
        for(i=0; i<3; i++)
        scanf("%d",pdizi[i]);
    
    
        for(i=0; i<3; i++)
        printf("%d\t%p\n",*pdizi[i],pdizi[i]);
    when i do it, i dont get any error
    it works
    nevertheless i want to know why it doesnt work when i get array values with pointers directly?

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by cena06 View Post
    hi =)
    i have tried to save array values with pointers but i got "segmentation fault".
    how can i do it corectly?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void main()
    {
        
        int *pdizi[3]={NULL};
        int i;
    
        for(i=0; i<3; i++)
        scanf("%d",pdizi[i]);
    
        for(i=0; i<3; i++)
        printf("%d\t%p\n",*pdizi[i],pdizi[i]);
    
    }
    pdizi is declared as an array of pointers - You initialise all the pointers to NULL

    You then try to put a value at the location of pdizi, which is pointing to NULL (You are not allowed to do this), so you get a seg fault.

    Changing pdizi to a non-pointer array will fix the issue. (Don't forget to use the '&' address of operator in the scanf)
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cena06 View Post
    nevertheless i want to know why it doesnt work when i get array values with pointers directly?
    I suspect you're making the classic beginner mistake of believing that a pointer and an array are the same things. They are not.

    The reason your second code sample works is because you have made the pointers in pdizi equal to the addresses of elements of dizi. Such pointers can be dereferenced as long as the array dizi exists.

    The first code sample does not work, because the elements of pdizi are NULL pointers. Dereferencing a NULL pointer gives undefined behaviour (of which a common symptom is a program crash, such as a segmentation fault).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to pointers with arrays
    By Nurumla in forum C Programming
    Replies: 3
    Last Post: 07-18-2011, 11:53 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. use pointers instead of arrays
    By amazonshopper in forum C Programming
    Replies: 7
    Last Post: 10-30-2007, 01:36 AM
  4. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  5. pointers and arrays
    By sscook69 in forum C++ Programming
    Replies: 2
    Last Post: 06-15-2002, 05:00 PM