LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-28-2004, 11:49 PM   #1
lawkh
LQ Newbie
 
Registered: May 2003
Posts: 19

Rep: Reputation: 0
pointer to pointer question in c


#include <stdio.h>
int main(int argc, char **argv)
{
char **q;
for (q = argv; *q; q++)
{
printf("next argv= %p\t: "%s"\n', *q, *q);
}
return 0;
}

I know char **argv, means argv contains the memory address of another pointer *argv which contains a memory address of a string.

Now here is my question, when i typed in something like "adad asdasd adasd",
the **argv seems that it can take in multiple arguments. Can someone explain this phenomenon? Isn't the parameter argv can only take one argument into the function.

p.s this is a lecture example, argc is not nessccary i guess.
 
Old 01-29-2004, 12:43 AM   #2
Stack
Member
 
Registered: Oct 2003
Distribution: FreeBSD
Posts: 325

Rep: Reputation: 30
usually you write is as char *argv[]

So this is actually a pointer to an array of charecter pointers. This is why you can take multiple arguments and the variable argc is the count of how many elements there are in the array.
 
Old 01-29-2004, 10:26 AM   #3
cjcuk
Member
 
Registered: Dec 2003
Distribution: Openwall, ~LFS
Posts: 128

Rep: Reputation: 15
`argv' (whether declared as **argv, *argv[] or argv[][]) is a pointer to an array of pointers. The easiest way to understand this is to swap from array notation to pointer arithmetic notation (making no functional difference, but easier to understand). So, if a variable is declared as `char **ptrptr' and contains ``adad asdasd adasd'', then `*(ptrptr + 0) == adad', `*(ptrptr + 1) == asdasd' and `*(ptrptr + 2) == adasd'. There is a convention (which argv follows) to then make the next entry null to signify the end of the list of pointers. To further clarify, our memory might end up like this:-

Code:
[cjc@locked ~]$ ./tmp this
   argv+0 : 0x5f914ce4
 *(argv+0): 0x5f914ddf, ./tmp
**(argv+0): 0x2e, .
 *(argv+1): 0x5f914de5, this
**(argv+1): 0x74, t
 *(argv+2): (nil)
EDIT: sorry if you read the previous version of this post, I typed quite a lot and made a slight mistake. It is corrected now. Also, I thought of a much more simple way to describe it =).

Last edited by cjcuk; 01-29-2004 at 01:42 PM.
 
  


Reply


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
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
hot to set value of pointer to pointer to a structure in C alix123 Programming 2 11-17-2004 06:40 AM
pointer question, djgerbavore Programming 6 08-13-2004 02:21 AM
yet another C pointer question fatman Programming 3 04-18-2003 04:59 PM


All times are GMT -5. The time now is 01:51 AM.

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