problem with pointer to array

Discussion in 'C Programming' started by ankitjain.bvcoe, Feb 17, 2006.

  1. Hi ,
    i don't know why the below mentioned program is giving wrong o/p.


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    ARRAY_SIZE 10
    int func(void *x)
    {
    int (*y)[ARRAY_SIZE];
    y=x;
    printf("%d, %d",y[0],y[1]);
    return 0;}

    int main (void)
    {
    int arr[ARRAY_SIZE]={1,2};
    int (*p)[ARRAY_SIZE];
    p=&arr;
    func(p);
    return 0;
    }


    O/P -10272722,-18928292
     
    ankitjain.bvcoe, Feb 17, 2006
    #1
    1. Advertisements

  2. I presume this was supposed to be "#define ARRAY_SIZE 10".
    y is a pointer to an array of 10 ints. y[0] is an array of 10 ints,
    which decays to a pointer to an array of 10 ints. You print this
    pointer value with a "%d" format, which expects an int, not a pointer.
    (I'm not 100% certain I've gotten all the details right.) Undefined
    behavior.

    Also, you should print '\n' character at the end of your output;
    otherwise it's not guaranteed that your output will appear.
    It's rarely makes sense to declare pointers to arrays. Usually you
    want a pointer to the element type; you can use such a pointer to
    access the rest of the array. You can pass the length of the array as
    a separate parameter.

    Here's a more straightforward way to do what you're trying to do:

    #include <stdio.h>

    #define ARRAY_SIZE 10

    int func(int *arr)
    {
    printf("%d, %d\n", arr[0], arr[1]);
    return 0;
    }

    int main(void)
    {
    int arr[ARRAY_SIZE] = {1,2};
    func(arr);
    return 0;
    }

    Output:

    1, 2
     
    Keith Thompson, Feb 17, 2006
    #2
    1. Advertisements

  3. ankitjain.bvcoe

    Markus Moll Guest

    Hi

    That won't compile. You probably meant "#define ARRAY_SIZE 10"
    Well, think about it: y is a pointer to array of int of size ARRAY_SIZE.
    Then y[0] is equivalent to (*y), which is an array of int.
    The pointer y does not refer to an array of anything, so y[1] doesn't make
    any sense.
    To get the first and second entry of _*y_, you have to write (*y)[0] or (*y
    [1], respectively.


    Markus
     
    Markus Moll, Feb 17, 2006
    #3
  4. ankitjain.bvcoe

    santosh Guest

    int (*y)[ARRAY_SIZE]; is not required.
    int *y = (int*)x ; is more than enough.
     
    santosh, Feb 17, 2006
    #4
  5. ankitjain.bvcoe

    al3x4nder Guest

    santosh напиÑав:
    in context func(), IMHO, int *y = (int*)x ; s not required as well,
    just
    change type of pointer in function proto to int * x, and them
    using it as x[0], x[1], that eq *(x + 0), *(x + 1).
    or i wrong?
     
    al3x4nder, Feb 17, 2006
    #5
  6. ankitjain.bvcoe

    Markus Moll Guest

    Hi
    Well... required... enough...

    If you want to pass (statically sized) arrays of ARRAY_SIZE ints, then I
    think it's best to use int (*y)[ARRAY_SIZE], as this will probably giva a
    type error warning if you pass a differently sized array.

    Markus
     
    Markus Moll, Feb 17, 2006
    #6
  7. thanks for your reply .

    there r still some confusions ....

    1) here y is a pointer to an array .
    let's say array is

    contents ----- >1 2 3 4
    5
    address ------>2000 2004 2008 2012 2016

    then y is pointer to this array therefore
    contents of y =2000
    add. of y = anything
    this is what i have checked
    So, now *y should be 1
    but its not,so why its not like this ..

    2) As per your comments..
    You don't use anything in either <stdlib.h> or <string.h>.

    Actually this was just a rough sample program related to my prj.So i
    just created
    a snapshot of the problem faced .
     
    ankitjain.bvcoe, Feb 17, 2006
    #7
  8. ankitjain.bvcoe

    Markus Moll Guest

    Hi

    Why that? (It probably "is", but: y does _not_ point to the first element of
    the array, but rather to the array itself. This means that what you get by
    dereferencing y is an array, not an element of the array.)
    No, *y should be an array, which is converted to a pointer to the first
    element.
    Because you ignore types in your reasoning.
    Yes, but you should really make your examples minimal. Including headers
    that aren't needed confuses the reader, because he tries to find out why
    they were included.

    Markus
     
    Markus Moll, Feb 17, 2006
    #8
  9. ankitjain.bvcoe

    Ichigo Kuler Guest

    Try to use (int (*x)[array_size]) to replace the ( void *x)
    maybe,it is more complex than yours, but it is working well and easy to
    understand.
     
    Ichigo Kuler, Feb 17, 2006
    #9
  10. I do not understand why you need to do it this way, but this compiles
    without warnings:

    #include <stdio.h>

    #define ARRAY_SIZE 10

    int func(void *x)
    {
    int (*y)[ARRAY_SIZE];
    y=x;
    printf("%d, %d\n",y[0][0],y[0][1]);
    return 0;}

    int main (void)
    {
    int arr[ARRAY_SIZE]={1,2};
    int (*p)[ARRAY_SIZE];
    p=&arr;
    func(p);

    return 0;
    }
     
    stathis gotsis, Feb 17, 2006
    #10
  11. ankitjain.bvcoe

    aisman Guest

    napisal(a):
    This is right:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    ARRAY_SIZE 10
    int func(void *x)
    {
    int *y;
    y=(int *)x;
    printf("%d, %d",y[0],y[1]);
    return 0;}

    int main (void)
    {
    int arr[ARRAY_SIZE]={1,2};
    int *p;
    p=arr;
    func(p);
    return 0;
    }


    or:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define ARRAY_SIZE 10
    int func(void *x)
    {
    int (*y)[ARRAY_SIZE];
    y=&x;
    printf("%d, %d",y[0],y[1]);
    return 0;}

    int main (void)
    {
    int arr[ARRAY_SIZE]={1,2};
    int (*p)[ARRAY_SIZE];
    p=&arr;
    func(*p);
    return 0;
    }
     
    aisman, Feb 17, 2006
    #11
  12. a écrit :
    Missing #define. Please don't retype, but copy & paste...

    #define ARRAY_SIZE 10
    pointers want dereferencment

    printf("%d, %d",*y[0],*y[1]);
     
    Emmanuel Delahaye, Feb 17, 2006
    #12
  13. ankitjain.bvcoe

    Chris Torek Guest

    [snip code with syntax error - obviously this was not the actual
    code!]

    You are using the type "pointer to array N of T", for some appropriate
    integer constant N and element-type T. Such a pointer can (and in
    your case does) point to the first of several such arrays:

    int (*p)[10];
    int arr[4][10];

    p = &arr[0];

    Now p points to the first of 4 arrays, i.e., arr[0]. Each of the
    four arrays is itself an array of size 10 of "int".

    This is a generalization of the fact that a pointer of type
    "pointer to T" (for any valid element type T) can point to
    the first of many objects of type T. This is why, for instance,
    we can do:

    char buf[SIZE];
    char *s = &buf[0];

    Now "s" points to the first element of buf, but s, for any
    valid integer i, names the array element buf.

    Note that the pointer "s" can point to just *one* item:

    char tmp;
    ...
    s = &tmp;

    after which only s[0] (which is the same as "*s") is valid. It
    can even point to *no* items at all:

    s = NULL;

    after which even s[0] is invalid.

    Given any pointer value "v", of type "pointer to T", there are
    several questions you need to answer to your own satisfaction --
    not necessarily with code, maybe just by *thinking* -- before using
    that value:

    - Is the value valid at all? (I.e., if "v" is a variable, has it
    been initialized? If it contains garbage, do not use it; just
    overwrite it with a valid value.)

    - Is it NULL?

    - If it is not null, does it point to an actual object, or the
    first of many objects (or perhaps even into the middle of a
    set of objects)?

    - If it points to the first (or middle) of a set of objects,
    how many objects are there around that location? (And how
    do you know?)

    See also <http://web.torek.net/torek/c/pa.html>.
     
    Chris Torek, Feb 17, 2006
    #13
  14. Or the equivalent but more common y[0][0] and y[0][1].


    Remove del for email
     
    Barry Schwarz, Feb 18, 2006
    #14
  15. Since x is a void*, the cast is unnecessary.


    Remove del for email
     
    Barry Schwarz, Feb 18, 2006
    #15
  16. ankitjain.bvcoe

    aisman Guest

    I made mistake:

    This is good


    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define ARRAY_SIZE 10
    int func(void *x)
    {
    int (*y)[ARRAY_SIZE];
    y=x;
    printf("%d, %d",*(*y+0),*(*y+1));
    return 0;}

    int main (void)
    {
    int arr[ARRAY_SIZE]={1,2};
    int (*p)[ARRAY_SIZE];
    p=&arr;
    func(*p);
    return 0;
    }
     
    aisman, Feb 18, 2006
    #16
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.