problem with pointer in array

This is a discussion on problem with pointer in array within the C Programming forums, part of the General Programming Boards category; can anyone show me the problem of the below code?it show segmentation fault.my idea is to let user input number ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    42

    problem with pointer in array

    can anyone show me the problem of the below code?it show segmentation fault.my idea is to let user input number to define the size of the array. please help!

    Code:
    #include<stdio.h>
    main()
    {	
    int a;
    int *i;
    int array[*i];
    int j;
    scanf("%d",&j);
    i=&j;
    scanf("%d",&array[0]);
    printf("%d\n",array[0]);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You can't do that with static memory.

    You need to malloc.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can. Just not the way he's trying to. C99 allows for variable length arrays.
    Code:
    #include<stdio.h>
    main()
    {	
    int a;
    int *i;  <-- doesn't point at anything specific 
    int array[*i  <-- dereferencing random spot in memory ]; 
    int j;
    scanf("%d",&j);
    i=&j;
    scanf("%d",&array[0]);
    printf("%d\n",array[0]);
    }
    That's why you're seg-faulting.


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

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by quzah View Post
    You can. Just not the way he's trying to. C99 allows for variable length arrays.
    Interesting. I think I saw that a few times and thought it was something non compliant with the standard.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    Interesting. I think I saw that a few times and thought it was something non compliant with the standard.
    Hi Cladiu...
    This is legal in c99...

    Code:
    int x = 30;
    int field[x];
    What our friend was trying, with an uninitialized pointer could result in an array gigabytes in size.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by CommonTater View Post
    Hi Cladiu...
    This is legal in c99...

    Code:
    int x = 30;
    int field[x];
    What our friend was trying, with an uninitialized pointer could result in an array gigabytes in size.
    Yes, I realized that his code was completely wrong.

    I have seen things like int field[n] before but I guess because I have been taught C in the stone age, I am not always aware of these fancy C99 things .
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    Yes, I realized that his code was completely wrong.

    I have seen things like int field[n] before but I guess because I have been taught C in the stone age, I am not always aware of these fancy C99 things .
    I tell ya, these new fangled inventions will be the death of me yet!

    I learned C on a C-99 platform (moving over from Pascal) but didn't know about this until I saw it here... go figger.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    42
    thanks all you guy. i got it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 11:00 AM
  2. Why does C need pointer conversion
    By password636 in forum C Programming
    Replies: 2
    Last Post: 04-10-2009, 08:33 AM
  3. Problem with Dynamically Increasing Array of Integers
    By laserlight in forum C++ Programming
    Replies: 30
    Last Post: 07-04-2008, 08:27 AM
  4. Returning pointer to array of pointers problem
    By jimzy in forum C Programming
    Replies: 15
    Last Post: 11-11-2006, 06:38 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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