简体   繁体   中英

C - Can I express char *argv[](command line aruments) as a character array for a struct variable?

I have a structure that I am assigning a value from bytes that are read in from an mp3 file

struct foo{
char name[30];  //name I want 
}

What I want to do is alter the struct variables from input via the command line arguments. This is how I initially set the struct variables:

int main(int argc, char *argv[]){
struct foo bar;
char *temp = argv[1];

fread(bar.name,1,30,file);//reading bytes from file and setting initial value


}

Here is a way two ways I've tried to alter them:

bar.name = temp;//   error: incompatible assigning (char[10] from char*)
strcpy(bar.name,temp);//  error: incompatible implicit declaration

This is what I type into command line: ./test new_title

How am I supposed to represent argv as a char[] rather than a char? I would use get_opt but we were asked not to.

regarding this question:

How am I supposed to represent argv as a char[] rather than a char? I would use get_opt but we were asked not to.

the argv[] is actually char**, so it is not any array of char.
Rather it is an array of pointers to char.

So argv cannot be treated as an array of char.

an array of char would look something like this:

char myArray[10].

an array of char arrays would look something like this:

char myArray[10][20]

neither of which matches this: (which is similar to what argv looks like)

char **myArray[10]
for i=0;i<9;i++
{
    myArray[i] = malloc( 20 );
}

myArray[9] = NULL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM