Conversión de argv al tipo de estructura

I have a structure defined as

struct xyz {

    char *str;
    int x;
    int y;

Which I am getting as input parameter to the executable from some other program1.(other program did execve de los program2 with input parameter as this structure).

I wish to know, can I do the typecast of this input parameter as (struct xyz*)argv[1];, or I have to convert it to string format before sending it?

preguntado el 08 de noviembre de 11 a las 08:11

Stupid question: Did you try ? Did it behaved as expected ? -

2 Respuestas

You can't pass arbitrary data to a command in that way. You'll have to serialize it to a string, or perform some IPC (e.g. through pipe/socket).

The reason for this is that the strings are null terminated. Your char* member will have a \0 on the end, and even if it doesn't, any int less than 16843009 (0x01010101) will have a null byte in it and fail to copy properly.

respondido 08 nov., 11:13

Note that integers larger than that number can have null bytes too. - Björn Pollex

Cierto. 0x7FFFFF00 will still screw it all up! - Polinomio

You also need a way to determine where the string in the command line ends and the numbers begin. - Karl Knechtel

@KarlKnechtel - That's what I meant by "serialize it to a string". - Polinomio

argv[1] is a string. You can't cast a string to a struct. You need to create your own wrapper functions to serialize and parse, and then watch out for all sorts of issues -- endian-ness issues and encoding issues (wide chars with Unicode, etc.)

respondido 08 nov., 11:13

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.