Thread: poniters will kill me

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804

    poniters will kill me

    Halo all,
    First of all, I would like to say that this thing called "pointers" is making me sick. Each time I see '*' I get totally confused as to what should I do. So please help me, you can say me a prey of pointers. I want them to understand like you guys here on this forum have done. But still I'm sure I dont know even 0.0001% of it. Here's a code which I cant understand
    Code:
    #include<stdio.h>
    #include<string.h>
    void f(int *a[])
    {
    	printf("%d",(*++a)[0]);
    	printf("\n%p",(void *)(*++a[0]));
    }
    int main(void)
    {
    	int i=10,j=20,k=40,l=50;
    	int *r[4];
    	r[0]=&i;
    	r[1]=&j;
    	r[2]=&k;
    	r[3]=&l;
    	f(r);
    }
    In this code except for knowing that r is an array of 4 int pointers, I dont know anything, specially when it's passed to the function. Now my questions are:
    1. what is a[] in the function?
    2.How are the outputs coming out to be 20 and CCCCCCCC respectively?
    3.When I try to initialize r like this
    Code:
    int *r[4]={&i,&j,&k,&l};
    I get warnings(j,k,l also)
    Warning 2 warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'i'
    I also want to say that this code wasn't given anywhere but I was exprimenting with array of pointers and suddenly made the code.
    Waiting in anticipation
    Thanks
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,183
    Quote Originally Posted by BEN10
    1. what is a[] in the function?
    Notation that hints that the argument is supposed to be a pointer to the first element of an array, in this case an array of pointers. You could have written without this hint:
    Code:
    void f(int **a)
    Quote Originally Posted by BEN10
    2.How are the outputs coming out to be 20 and CCCCCCCC respectively?
    When control enters f, a is a pointer to the first pointer in r from main. The result of ++a is therefore a pointer to the second pointer in r from main. Hence, the result of *++a is the second pointer in r from main. This means that (*++a)[0] (equivalent to *((*++a) + 0)) is what the second pointer in r from main points to, i.e., j from main. Since j has a value of 20, 20 is printed.

    Now, a is a pointer to the second pointer in r from main. The result of a[0] is therefore what a points to, i.e., the second pointer in r from main. On the next step, we have undefined behaviour: ++a[0] is then the next pointer in some array that the second pointer in r is supposed to point into, but the second pointer in r points to j, which is a variable by itself. Chances are, what you will end up with is the address of a variable adjacent on the stack that j is on, e.g., you might end up with the address of i, or maybe that of k. Of course, then *++a[0] is the corresponding value, e.g., 10 or 40 (but remember: we are in the realm of undefined behaviour). So, the cast just casts an int value to void*, then prints it as if it were an address.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Thanks laserlight for your explanatory response. Now I understand it. But what about my third question. I repeat it here. When I initialize the array of pointers I get warnings:
    Code:
    Warning	1	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'i'		
    Warning	2	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'j'		
    Warning	3	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'k'		
    Warning	4	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'l'
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BEN10 View Post
    But what about my third question.
    I've never tried to do that but evidently you can't, which is a bit of a drag. There are other little no-no's about variable initialization like that, I imagine it has to do with how the compiler works.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    In other words, you are asking the compiler (at compile time) to know what addresses those four variables will have (at runtime), which of course it can't.

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by tabstop View Post
    In other words, you are asking the compiler (at compile time) to know what addresses those four variables will have (at runtime), which of course it can't.
    Thanks tabstop that makes sense. Now I get the whole thing.
    Now something off topic.
    How do all of you professionals are so genious in programming? All of the answers and concepts are on your tips. Be it a toughest of tough question you are there to crack it. I still just imagine what it takes to become a professional in coding. Seriously, all of you people in this forum are genious. Hats off.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BEN10 View Post
    Seriously, all of you people in this forum are genious. Hats off.
    Someone please give BEN10 a job hiring programmers, maybe at a nice company in the Bahamas...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Most here have been programming for years. That's how they seem to know everything.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by BEN10 View Post
    Thanks tabstop that makes sense. Now I get the whole thing.
    Now something off topic.
    How do all of you professionals are so genious in programming? All of the answers and concepts are on your tips. Be it a toughest of tough question you are there to crack it. I still just imagine what it takes to become a professional in coding. Seriously, all of you people in this forum are genious. Hats off.
    Because we encountered the same problems you have, and put an unreasonable amount of time into finding a solution
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by BEN10 View Post
    Thanks laserlight for your explanatory response. Now I understand it. But what about my third question. I repeat it here. When I initialize the array of pointers I get warnings:
    Code:
    Warning	1	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'i'		
    Warning	2	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'j'		
    Warning	3	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'k'		
    Warning	4	warning C4221: nonstandard extension used : 'r' : cannot be initialized using address of automatic variable 'l'
    What compiler are you using? I can't reproduce that error
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Visual C++.
    And it's a warning. Albeit a non-standard code warning.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by Elysia View Post
    Most here have been programming for years. That's how they seem to know everything.
    For how many years are you programming? And what approach did you take for difficult to understand topics specially pointers?
    Quote Originally Posted by ಠ_ಠ View Post
    Because we encountered the same problems you have, and put an unreasonable amount of time into finding a solution
    I also devote time in thinking but to no avail. If I would have not joined this forum I would be still using void main(). Seriously.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by BEN10 View Post
    And what approach did you take for difficult to understand topics specially pointers?
    I'm still not Elysia but my #1 recommendation for understanding pointers is linked lists. IMO you are right to hang around cboard. Sometimes it seems like a lot of nitpicking (like the void main thing) but all those nits picked add up to "one big ball of wax". Also, you get a chorus of opinions, which you won't find in a book written by only one or two people.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by MK27 View Post
    I'm still not Elysia but my #1 recommendation for understanding pointers is linked lists.
    Yeah I was referring this question in general to all. Ok I was thinking of going to data structure soon after finishing my C basics but everytime I get stuck at one point or the other. Thanks, now after your reply I think I'll soon switch to linked lists.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by tabstop View Post
    In other words, you are asking the compiler (at compile time) to know what addresses those four variables will have (at runtime), which of course it can't.
    Of course it can, though. The addresses of those local variables are just offsets from the current base pointer and the instructions to produce those addresses can be generated at compile time.

    I have no idea why that initialization is non-standard. It isn't like it's hard for the compiler to implement it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to kill a dragon with various programming languages
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-01-2007, 12:13 PM
  2. Kill Signal Number? - Help !
    By brett in forum C Programming
    Replies: 5
    Last Post: 06-20-2007, 03:39 AM
  3. Can anyone help me with the Kill command in C please?!!!
    By jon20 in forum Linux Programming
    Replies: 3
    Last Post: 04-07-2002, 10:37 AM