Dear All,

I am very new to programming and really appreciate if you can help. I am using XCode as editor

My code is below:

int main (int argc, char * const argv[]) {
	char a[10][25];
	char *ptr_a;
	ptr_a = (char *)a[10][25];
	for (int index = 1; index < argc; index++) 
			 {
 std::cout << index << " " << "The value in argv[index] is : " << argv[index]<< std::
strcpy(ptr_a,argv[index]);
std::cout << index <<  " " << "the value of a at this point is : " << a << std::endl;
}
			return 0;
}

After this when i run the program i dont get any result. Basically what i am trying to achieve here is to copy argv[index] to a[10][25] and it doesnt work. I know i am making a mistake but dont know where, would guess somethings with pointers.

Your help will be appreciated.

P.S I am new to programming and not sure if i have asked the question correctly. But on the way of learning so please do let me know if you need to some more information.

Thanks :-)

Recommended Answers

All 13 Replies

First of all argc should be initialized.

commented: wrong +1

First of all argc should be initialized.

Wrong.... The variable argc is an integer initialized by the o/s before being sent to main() that's why it's a parameter of the function. All you have to do is use it...

P.S.
You're lucky I messed up on the like-dislike.

@OP:
Try changing Line 4 to simply ptr_a = a ;

Hey Guys,

Thanks a lot for the replies. But when i change the line 4 to
ptr_a =a;

I get the error message "Error: cannot convert 'char [10][25] to 'char*' in assignment.

But i got it working without a pointer :

int main (int argc, char * const argv[]) {
	
	char a[10][25];
	
	
	for (int index = 1; index < argc; index++) 
			 {
		 		
				 std::cout << index << " " << "The value in argv[index] is : " << argv[index]<< std::endl;
				 strcpy(a[index],argv[index]);
				 std::cout << index <<  " " << "the value of a at this point is : " << a[index] << std::endl;
			 
			 }
			return 0;
	
	}

But ofcourse i would like to know how to access the memory location with pointers. Will start reading it all again :-)

Once again thanks a lot for your help

> char a[10][25];
> char *ptr_a;
> ptr_a = (char *)a[10][25];
If you really want to do this with pointers, then it's

char a[10][25];
	char  (*ptr_a)[25];
	ptr_a = a;

Then you should be able to use strcpy( ptr_a[index], argv[index] );

Thanks anyway.I deserve it.From what i know if you don't specify the number of arguments it wont' work.But then again .. maybe you're more well informed.Try to explain this better please.

Thanks anyway.I deserve it.From what i know if you don't specify the number of arguments it wont' work.But then again .. maybe you're more well informed.Try to explain this better please.

Who are you addressing, and what are you talking about? Are you referring to argc and argv[]?

When you start a program, they give you the ability to send command-line arguments to the program. The value of argc is the count of the number of arguments and the values of argv[] are the contents/values of the arguments. Both are automatically initialized when the program starts.

The structure of argv is an array of char arrays. The contents of the sub-arrays are then analyzed to determine what the entered information is.

Ok i do know that.But when not intitializing argc , the parameter(s) passed at the command line won't be processed by the main program.It really makes sense since you array's index will be uninitialized.

Ok i do know that.But when not intitializing argc , the parameter(s) passed at the command line won't be processed by the main program.It really makes sense since you array's index will be uninitialized.

The value of argc will always be >= 1 and argv will always have at least argc[0]. This is because the execute statement on the command line is considered the first argument.

Study passing arrays to functions a little more. It is allowable to leave the size of the first dimension of an array un-specified in a function's parameter list. Under these circumstances, when the function is called the size of the array is "dynamically" determined (sort of).

Hi Salem,
Did try the possibility to mentioned but i got error messages
1) invalid conversion from char to char*
2)initializing argument 1 of char* strcpy(char*, const char*)

Thanks

The value of argc will always be >= 1 and argv will always have at least argc[0]. This is because the execute statement on the command line is considered the first argument.

Study passing arrays to functions a little more. It is allowable to leave the size of the first dimension of an array un-specified in a function's parameter list. Under these circumstances, when the function is called the size of the array is "dynamically" determined (sort of).

Ok . So why won't it work?I'm not disagreeing with you but that argument should.I can't see the purpose of the int argc argument in the main function if the os initializes the count automaticaly as you say.I mean .. i would not write an accesor for example , with an extra argument.Should it not be

int main (char* argv[])

supposing you're right ?

Ok . So why won't it work?I'm not disagreeing with you but that argument should.I can't see the purpose of the int argc argument in the main function if the os initializes the count automaticaly as you say.I mean .. i would not write an accesor for example , with an extra argument.Should it not be

int main (char* argv[])

supposing you're right ?

No, cause then argc flat out wouldn't exist. The values in argc and argv must be used together. The value contained in argc is comparable to an "arraySize" variable that woud typically be passed to any other function that accepts an array as a parameter and is used similarly.

The problem isn't with argc and argv[], it's with the assignment of the pointer to a[0][0] to ptr_a. It's completely unrelated. I think it best you start a new thread if you wish to pursue this topic further, we've already hijacked this thread enough.

char* argv[]

Is a pointer to an array of pointers.Or a pointer to an array of c character strings.If you don't initialize argc which should be the index for argv , pointing to a c string , then you wouldn'nt have an argument for the main function.
From what i saw .. declaring a data type does not by default initializes it with zero .. so in this case we do not have a string.Thus no argument.

char* argv[]

Is a pointer to an array of pointers.Or a pointer to an array of c character strings.If you don't initialize argc which should be the index for argv , pointing to a c string , then you wouldn'nt have an argument for the main function.
From what i saw .. declaring a data type does not by default initializes it with zero .. so in this case we do not have a string.Thus no argument.

I said start a new thread. I will not continue this here.

commented: Yes. +5
commented: I'll give you the +rep instead of giving him the - rep even though it is deserved +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.