Question about pointers

Discussion in 'C Programming' started by silusilusilu, Sep 19, 2013.

  1. silusilusilu

    silusilusilu Guest

    Hi,
    i have some troubles with a code like this:

    void main (void)
    {
    char array[2];
    function1(array);
    }

    void function1(char * data)
    {
    ....
    function2(data)
    }

    function2(char * func2data)
    {
    ...
    }

    I know that function2 works well, but in this case array memory address seems lost: the array memory address before funtion1 is different from its address after function1
    What's wrong?
     
    silusilusilu, Sep 19, 2013
    #1
    1. Advertisements

  2. silusilusilu

    John Gordon Guest

    Show us the code you're using to display the array addresses.

    I'll make a guess that within each function you're displaying the address
    of the local pointer itself, instead of what it points to.
     
    John Gordon, Sep 19, 2013
    #2
    1. Advertisements

  3. silusilusilu

    James Kuyper Guest

    The standard specifies two ways of defining main(); you can use any
    other method that is equivalent to one of the two standard ways. Both
    methods return an 'int'. Declaring 'int' to return anything other than
    'int' means that a C compiler is not required to accept your code.
    At this point, there's no declaration in scope for function1(). In C90,
    it would be implicitly declared as returning 'int' and taking a fixed
    but unspecified number of parameter of unspecified types; this was a bad
    idea, because it's very error prone, which is why it's no longer allowed
    in C99.
    You should provide a function prototype. The simplest way, and the one I
    prefer, is to define function1() before defining main() - the first part
    of the function definition qualifies as a function prototype prototype.
    Other people find it confusing to declare functions before calling them,
    in which case just write a prototype that is not a definition:

    void function(char *);
    There's a missing ';'.
    You didn't declare a return type for function2, which is another
    indication that you're writing for C90. In C99 and later, such
    declarations are no longer allowed. I recommend providing a function
    prototype.
    I'm not sure why you think the address has changed; if you could provide
    the evidence you used to reach that conclusion, we can probably explain
    to you why that evidence doesn't mean what you thought it meant.

    Here's a minor re-write of your code:

    #include <stdio.h>
    static void function2(char * func2data)
    {
    printf("funcdata:%p &funcdata:%p\n",
    (void*)func2data, (void*)&func2data);
    }

    static void function1(char * data)
    {
    printf("data:%p &data:%p\n", (void*)data, (void*)&data);
    function2(data);
    }

    int main (void)
    {
    char array[2];
    function1(array);
    printf("array:%p &array:%p\n", (void*)array, (void*)&array);
    return 0;
    }

    Here's the results I got:
    data:0xbf95577e &data:0xbf955760
    funcdata:0xbf95577e &funcdata:0xbf955740
    array:0xbf95577e &array:0xbf95577e

    All of the pointers that should be equal printed out the same; all of
    the pointers that should be different printed out differently, so that
    output matches my understanding. If anything about it seems odd, let us
    know, and someone will be able to explain it to you.
     
    James Kuyper, Sep 19, 2013
    #3
  4. silusilusilu

    Seebs Guest

    Yes, yes you do.
    Probably irrelevant, but this declaration is incorrect. main() returns
    an int.
    And how do you know that?
    And how do you know that?
    How are you determining this?
    What's wrong is you've provided insufficient information to give
    any information about what happened, although I have a guess. I note
    also that you are using different names in each function, so you may not
    be aware that each function sees only its own argument list; you could
    use the same names for these variables everywhere. (Although I won't in my
    test case because I suspect it'll be easier to describe what's happening
    with unique names for everything).

    First, let's make a program that would actually illustrate this:

    #include <stdio.h>

    void func2(char *data2) {
    printf("in func2: data2 is %p\n", (void *) data2);
    }

    void func1(char *data1) {
    printf("in func1, pre: data1 is %p\n", (void *) data1);
    func2(data1);
    printf("in func1, post: data1 is %p\n", (void *) data1);
    }

    int main(void) {
    char array[2];
    printf("in main, pre: array is %p\n", (void *) array);
    func1(array);
    printf("in main, post: array is %p\n", (void *) array);
    return 0;
    }

    My guess is, if you run this, you will find that all five lines of
    output produce the same value. And the array's address doesn't change...

    But!

    It might be that the local value "data1" in func1 is being changed. And
    if, in func2, you did something to the contents of data2, which overran
    that array, on some systems that might even be fairly likely. Or things
    might go horribly wrong in other ways. But since you didn't show any
    actual working code, no one can tell.

    -s
     
    Seebs, Sep 20, 2013
    #4
    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.