In linux system why this?

Discussion in 'C Programming' started by smartbeginner, Dec 26, 2005.

  1. I am not able to use pointer to array in Linux .It always shows error
    when I try to read from keyboard or print to screen.
    main()
    {
    int i,j;
    int (*a)[2];
    a=(int *)calloc(2,sizeof(int));
    for(i=0;i<2;i++)
    for(j=0;j<2;j++)
    scanf("%d",(*(a+i)+j));
    }
    This shows segmentation fault?Why is this happening
    But array of pointers works.
     
    smartbeginner, Dec 26, 2005
    #1
    1. Advertising

  2. smartbeginner

    Scorpio Guest

    smartbeginner wrote:
    > I am not able to use pointer to array in Linux .It always shows error
    > when I try to read from keyboard or print to screen.
    > main()
    > {
    > int i,j;
    > int (*a)[2];
    > a=(int *)calloc(2,sizeof(int));


    casting is redundant for calloc because it returns void* type.

    > for(i=0;i<2;i++)
    > for(j=0;j<2;j++)
    > scanf("%d",(*(a+i)+j));
    > }
    > This shows segmentation fault?Why is this happening


    Because you are accessing memory location that does
    not belong to your program. You are allocating only enough memory
    for 2 integers and scanning into 4 integers.

    > But array of pointers works.


    I don't know how it worked, show us the code.
     
    Scorpio, Dec 26, 2005
    #2
    1. Advertising

  3. smartbeginner

    John Bode Guest

    smartbeginner wrote:
    > I am not able to use pointer to array in Linux .It always shows error
    > when I try to read from keyboard or print to screen.
    > main()
    > {
    > int i,j;
    > int (*a)[2];
    > a=(int *)calloc(2,sizeof(int));


    This cast is both wrong and unnecessary. Remove it from the statement.


    Actually, it would be better just to write

    a = malloc(sizeof *a);

    Much simpler.

    > for(i=0;i<2;i++)
    > for(j=0;j<2;j++)
    > scanf("%d",(*(a+i)+j));
    > }
    > This shows segmentation fault?Why is this happening


    Because you are attempting to access memory outside of what you've
    allocated. You have allocated an array of 2 ints, (*a)[0] and (*a)[1].
    When both i and j are 1, you are trying to write to (*a)[2], which is
    outside of the memory you allocated.

    > But array of pointers works.
     
    John Bode, Dec 26, 2005
    #3
  4. smartbeginner wrote:
    > I am not able to use pointer to array in Linux .It always shows error
    > when I try to read from keyboard or print to screen.
    > main()
    > {
    > int i,j;
    > int (*a)[2];
    > a=(int *)calloc(2,sizeof(int));
    > for(i=0;i<2;i++)
    > for(j=0;j<2;j++)
    > scanf("%d",(*(a+i)+j));
    > }
    > This shows segmentation fault?Why is this happening
    > But array of pointers works.
    >


    Your code contains lots of errors. What sources have you used when
    learning C, I'm just curious?


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
     
    August Karlstrom, Dec 26, 2005
    #4
  5. Hello Sir,

    Can you please point out my errors.So that I can study C better.Thank
    you for your speedy reply
     
    smartbeginner, Dec 26, 2005
    #5
  6. "smartbeginner" <> writes:
    > Hello Sir,
    >
    > Can you please point out my errors.So that I can study C better.Thank
    > you for your speedy reply


    Many of your errors are documented in <http://cfaj.freeshell.org/google/>.
    Fix those, and then we can talk about errors in C.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 26, 2005
    #6
  7. On 26 Dec 2005 05:06:24 -0800, "smartbeginner" <>
    wrote:

    >I am not able to use pointer to array in Linux .It always shows error
    >when I try to read from keyboard or print to screen.
    >main()
    >{
    >int i,j;
    >int (*a)[2];
    >a=(int *)calloc(2,sizeof(int));


    If your compiler did not issue a diagnostic for this statement, you
    need to correct your configuration parameters or get one that deals
    with the C language. The right hand side of your statement has type
    pointer to int (courtesy of the cast). The left hand side has type
    pointer to array of two int (from the definition of a). There is no
    implicit conversion between these types. This is a constraint
    violation requiring a diagnostic.

    While we recommend not casting the return from the allocation
    functions, in this case your cast serves to indicate that you do not
    understand the type of a.

    If you remove the cast (and remember to include stdlib.h), the
    statement should allocate space for one array of two int. If you had
    really intended to allocate space for two arrays, you should follow
    the recommended practice of
    a = calloc(2, sizeof *a);
    In this case, malloc may be more appropriate since you never use the
    zero values that calloc stores in the allocated area.

    >for(i=0;i<2;i++)
    >for(j=0;j<2;j++)
    >scanf("%d",(*(a+i)+j));


    Here is where you introduce undefined behavior. You should consider
    yourself fortunate that the UB manifested itself in an immediately
    obvious manner. This discussion group is full of queries from
    confused people who were not so fortunate.

    Variable a is a pointer to an object (in this case an array of two
    int.)

    The expression a+i involves pointer arithmetic. (Pointer
    arithmetic is different than "normal" arithmetic because the size of
    the object pointed to participates. If p is a pointer to an object of
    size 5, then p+i evaluates to an address that is 5*i bytes greater
    than the value in p.) The expression evaluates to the address of the
    i-th object after the one pointed to. Note that expression a+0, or
    the equivalent a, evaluates to the address of the object pointed to,
    which is exactly what we mean when we say a is a pointer to an object.

    *a and the equivalent *(a+0) evaluate to the object, in this case
    an array of two int.

    *(a+i) evaluates to the i-th object after the one pointed to by a.
    Unfortunately, in this case there is no such object whenever i is
    greater that 0. You only allocated space for one array.

    >}
    >This shows segmentation fault?Why is this happening
    >But array of pointers works.


    An array of pointers can work. A pointer to an array can work. A
    dynamic 2-dimensional array pointed to by an int** can work. There
    are probably several more approaches that can work. All you need is
    the correct code for the approach you choose.


    <<Remove the del for email>>
     
    Barry Schwarz, Dec 27, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    907
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,984
    Smokey Grindel
    Dec 2, 2006
  3. markus
    Replies:
    22
    Views:
    667
    Dances With Crows
    Sep 22, 2004
  4. Nagaraj
    Replies:
    1
    Views:
    871
    Lionel B
    Mar 1, 2007
  5. fmbright
    Replies:
    8
    Views:
    215
Loading...

Share This Page