Friday, 13 September 2013

creating memory of the size of a pointer for char array inside struct

creating memory of the size of a pointer for char array inside struct

I have a program that asks to input two arguments using argv and I would
like to then using the length of argv[1] and argv[2] dynamically allocate
memory for two pointer char inside my struct the size of argv[1] and
argv[2]. Here is my code but i'm not sure if i did it correctly, can
anyone verify? Parameters passedargv1 and passedargv1 inside the function
are argv[1] and argv[2] passed from the main function. Basically i want to
make chararray1=argv[1] and chararray2=argv[2] if they were just plain
char arrays but i don't know ahead of time the size of our input so i
can't pre-initialize chararray2 and chararray2. Also i can't change what
is inside argv so i cannot have chararray1 and chararray2 just point to
them because i will need to change them later on.
struct StructInformation{
char *chararray1;
char *chararray2;
};
typedef struct StructInformation SimplifiedStruct;
SimplifiedStruct *CreateMem(char *passedargv1, char *passedargv2) {
SimplifiedStruct *ptr=(SimplifiedStruct*)malloc(sizeof(SimplifiedStruct));
ptr->chararray1=(char*)malloc(sizeof(passedargv1));
ptr->chararray2=(char*)malloc(sizeof(passedargv2));

No comments:

Post a Comment