Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing arguments to main function.

Status
Not open for further replies.

scecilia

Programmer
Jul 8, 2002
35
US
Dear Members,

I have a very puzzling problem. I have a tiny C program that I want to run with the following command line argument at the prompt:
>> Pointers 3

Pointers.c looks like:

void main(int count, int *argv[]) {
int *ptr;
ptr = &argv[1];
printf("\n*** Parameter passed is: %d \n", *ptr);
}

So I want the printf to print the '3' I am passing, but instead a get a weird number!!! I tried to remove the '&' when I do the ptr assignment but nothing seems to work. I thought I had understood pointers :-( Can you help?

--Cecilia.

 
Try,

void main(int count, char* argv[])

I have never seen it as int* so I dont know if it is acceptable. I would assume not. If you know for a fact that argv[1] is a number then use

int val;
val = atoi(argv[1]);

printf("\n*** Parameter passed is: %d \n", val);

Matt
 
That worked great Matt, Thank You. I think the solution works because string arrays are passed by value. However, I am very puzzled of why it would not work if the arguments passed are integers. Neither of these seems to work:

main(int count, int *argv[])
main(int count, int argv[])

For this last one, it seems to me that a simple thing like accessing directly argv[1] should give you the parameter passed! Like in: int x = argv[1]; Why that does not work??? Hummm.

 
I think that it comes in as a string no matter what. The fact that you have it as an int would mean that if you made the first agument a sigle digit like 9, you would get a return value of ascii 9 or 57. You can verify this by entering in a single character and finding out what it is when processed. This would be my guess.

Make sure it is a single character however because I think there are issues when an int is set to a string.

Check this and lemme know what happens :)

Matt
 
It probably doesn't work for the simple reason that it's invalid. An array of char* is not the same thing as an array of int* is not the same thing as an array of int.

If you treat it as an array of int, you'll get the addresses in memory that contain "Pointer", "3", and "". If you converted these back into char*, you could access the arrays... but do it right the first time.

If you treat it as an array of int*, you'll get a 2D array of integers. Assuming an int is 4 bytes and a char is 1 byte, the first row will contain an integer that's equal to the ASCII value of 'n' + 2^8 x the ASCII value of 'i' + 2^16 x the ASCII value of 'o' + 2^24 x the ASCII value of 'P', and another integer that's equal to the ASCII value of '\0' + 2^8 x the ASCII value of 'r' + 2^16 x the ASCII value of 'e' + 2^24 x the ASCII value of 't'. The second row will contain a mostly random number that consists of the ASCII value of 2^24 x the ASCII value of '3' + whatver happens to be in the last 2 bytes (the null will add 0). You'll get that number if you access it, provided you don't get a segmentation fault. The final row contains a similarly random and inaccessable integer whose high-order byte is entirely zero.

Thus, aside from those declarations being invalid, they have no really useful information.
 
Thank you chipperMDW, I guess I was trying to bend the rules of parameter passing to a C main function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top