Thread: How come argv can be incremented?

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    How come argv can be incremented?

    Hi..


    we all very much know the base address of an array cannot be incremented ...

    i mean if i declare something like this,
    Code:
     char *a[10];  
    a++;


    but the code
    Code:
     int main(int argc, char *argv[])
    {
    
    
    printf("%s", *++argv);
    
    
    }
    was running without any compiler error..



    some body please explain this...


    thanks and regards..
    raj_ksrt.

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    a pointer to the base address of an array can be incremented.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    argv is a pointer to the first element of an array of pointers to char... until you increment it, in which case it becomes a pointer to the second element of the same array of pointers to char.
    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

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    i am puzzled, though.

    if a is an array of char *s, why doesn't the OP's first expression point to the first char * in a?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    why doesn't the OP's first expression point to the first char * in a
    it does, but it is still array and cannot be incremented
    if its value is assigned to the pointer, or passed as a function parameter - we get the pointer with the same value, pointer can be incremented
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    if a is an array of char *s, why doesn't the OP's first expression point to the first char * in a?
    Because a is an array of pointers to char, not a pointer to the first element of an array of pointers to char. When passed as an argument, an array is converted to a pointer to its first element.
    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

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am starting to see way more questions such as this on the forums. Perhaps its time to whip up an in depth explanation of the indirection of pointers. I am not surprised this is so complicated to many these days though. I notice schools are leaning heavily on teaching C# and Java to entry level computer science students.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by master5001 View Post
    I am starting to see way more questions such as this on the forums. Perhaps its time to whip up an in depth explanation of the indirection of pointers. I am not surprised this is so complicated to many these days though. I notice schools are leaning heavily on teaching C# and Java to entry level computer science students.
    Confusion about pointers is nothing new around here. However, there is indeed an oddity in the language with regard to this particular question. Consider this:

    Code:
    int foo(int *blah[10])
    {
        int *fuzz[10];
        blah++;
        fuzz++;
    }
    Even though the two variables fuzz and blah seem to be declared in EXACTLY the same way, they ARE NOT THE SAME. The increment of blah is allowed. The increment of fuzz is not.

    I do not blame people for having a hard time understanding why.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Which is why I always advertise not using the [] syntax in function arguments. Far less confusing that way.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    >>I do not blame people for having a hard time understanding why.

    I suppose you have a good point on that one. This is one of the reasons iterators were developed.

    >>Which is why I always advertise not using the [] syntax in function arguments. Far less confusing that way.

    Kudos to that...

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To add another one to the list:
    http://cpwiki.sourceforge.net/A_pointer_on_pointers

    Though they are so incredibly simple, they can also be mind boggling. One needs to know the basics and work from that, not trying to see pointers like some super powerful, super flexible solution to everything. They work just one way, not multiple ways.
    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.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To be honest, I think the problem is that pointers as we know them are based on the programming concepts of C and people learn how to read and think in C++ nowadays. brewbuck's example is not very intuitive for any type of understanding though. I think a lot of benefit could be gained through teaching students about C++'s alternatives (which I never use, despite my advocation of them) to pointers.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know if I agree. Pointers are still very important, even in C++.
    It's absolutely a good thing to know about pointer basics. My own guess is that books don't cover the basics of pointers (they merely say, if you want another function to modify an argument, then do this - it's called pass by pointer) or they simply do not understand it and skips the basics.
    Or it could be the teachers' fault. We know how many bad teachers are out there.

    And I don't know if people are really thinking C++ these days... I've messed around in a project that is very "C+"! Basically, it heavily utilizes classes but never (or very rarely) uses the standard library. Everything is practically made C.
    C with classes :O
    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.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am not shirking the importance of pointers. I am simply saying that its not something that seems very well explained to people these days, which is truly unfortunate. And the even more unfortunate thing is that despite not receiving enough information about pointers, they are also not receiving enough information about how to accomplish the tasks that pointers complete.

    I think pointers are best explained graphically. In fact some of the best illustrations I've seen regarding pointers were in an assembler tutorial I read way back in the day that assumed a competent understanding of C. Despite that, the images were something I recognized as useful for someone who "doesn't get pointers."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. argv
    By taurus in forum C Programming
    Replies: 15
    Last Post: 10-14-2007, 08:57 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 06:09 AM
  3. Converting a argc / argv construct into a va_list
    By chambece in forum C Programming
    Replies: 6
    Last Post: 07-03-2005, 04:47 PM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM