Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-08-2013, 01:30 PM   #1
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 620
Blog Entries: 3

Rep: Reputation: 273Reputation: 273Reputation: 273
C: Question on pointers


I have a small test program that looks like this:
void fill_arr(int *pointer, int size);

int main()
    int array[SIZE] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int *pointer;
    pointer = array; // pointer = array[0]

    fill_arr(pointer, SIZE);
    /* Let's try and print the above... */
    int k;
    for (k=0; k<SIZE; k++) {
        printf("Array value %d: %d\n", k, array[k]);

    printf("Address of pointer: %p\n", pointer);
    printf("Address of array[0]: %p\n", &array[0]);

    return 0;

void fill_arr(int *pointer, int size)
    int i;
    for (i=0; i<SIZE; i++) {
        *pointer = (rand()%10)+1;
        pointer++; // move index by one to array[1], array[2], etc...
With the following output:
Array value 0: 10
Array value 1: 10
Array value 2: 7
Array value 3: 6
Array value 4: 7
Array value 5: 8
Array value 6: 6
Array value 7: 1
Array value 8: 6
Array value 9: 6
Address of pointer: 0xbfd9db20
Address of array[0]: 0xbfd9db20
Now, my (stupid?) question is:
How can the address of the pointer 'pointer' be the same as array[0] AFTER I have added to it in the loop like this:
pointer++; // move index by one to array[1], array[2], etc...
It would seem more logical to me that pointer would point to array[9] after the function. If someone could help me solve this little riddle I would appreciate it.

Old 09-08-2013, 02:45 PM   #2
Registered: Jun 2009
Location: kolkata,India
Distribution: Mandriva,openSuse,Mint,Debian
Posts: 285
Blog Entries: 1

Rep: Reputation: 37
well, this is merely a question of call-by-value or call-by-reference.
actually when you make the call
fill_arr(pointer, SIZE);
the "pointer" is the actual parameter.

when it comes to
void fill_arr(int *pointer, int size)
the "pointer" becomes the dummy variable.
For the sake of simplicity let's name the pointer in "fill_arr(pointer, SIZE);" as pointer1
and the pointer in "void fill_arr(int *pointer, int size)" as pointer2. so pointer2 is local to
the function fill_arr. when you make the call to the function the value of pointer1 gets copied into pointer2.
now this pointer2 is manipulated and not the pointer1. but in the main function it is still the pointer1 version.

I hope you got it now.
1 members found this post helpful.
Old 09-09-2013, 12:49 AM   #3
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Xubuntu, Manjaro
Posts: 620
Blog Entries: 3

Original Poster
Rep: Reputation: 273Reputation: 273Reputation: 273
Originally Posted by lemon09 View Post
I hope you got it now.
I do! Thanks for pointing out the obvious. Very much appreciate the time you took to explain.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] A question about pointers in C... trist007 Programming 3 07-11-2011 10:20 PM
C: structs and pointers question kpachopoulos Programming 3 03-27-2006 05:54 PM
question about pointers vijeesh_ep Programming 1 09-13-2004 03:13 PM
Another C pointers question from a newbie chris.hicks Programming 6 11-11-2003 05:14 AM
question on pointers h/w Programming 1 10-26-2003 02:54 PM

All times are GMT -5. The time now is 05:17 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration