Hello. This is my first post, and I am fairly new to C.

I am writing a socket program and I am getting a segmentation fault upon runtime. My program compiles fine. I'm pretty sure it is due to my implementation of my data structure.

My struct looks something like this:

typedef struct{
char* array;
}mystruct;

In main, can I assign a dynamic array like:

main(int argc,char *argv[]){
mystruct* A;
A->array = (char*) malloc(sizeof(argv[2])*sizeof(char));
}

Thank you for help!

M.

Recommended Answers

All 3 Replies

mystruct* A;

You define A as a pointer to mystruct but never assign or allocate memory for it.

Thank you. That was the problem. I really appreciate it.

Also what are you expecting to get when you do sizeof(argv[2]). It will give you the size of a pointer. Is that what you want ?

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.