typedef struct What_if
{
char price[2];
} what_if ;
what_if *what_if_v开发者_如何学运维ar;
int main(int argc,int argv[])
{
int m= argv[1];
what_if_var[m]='\0';
format_input_records();
getch();
return 0;
}
int format_input_records()
{
strcpy(what_if_var[0].price,"sss") ;
printf("\ntrans_Indicator ==== : : %s",what_if_var[0].price);
return 0;
}
here i need dynamic value for structure array size ?how can i achieve this plz help me?
First, you have a problem in that I don't think you understand what a pointer is:
what_if *what_if_var;
...
what_if_var[m]='\0';
You created a pointer to a What_if
struct, never allocate anything, then try and use it (and as an array of them)
You also don't have the correct signature for main()
int main(int argc, char* argv[])
As noted by a direct comment to your question, this is an indication that you really don't understand the basics of the language, and an introductory book is in order.
That being said, what you need is an array of your structs the size of which is passed in via argv
(after converting it from char* to int), and inside the struct you'd need to have a char *
pointer. For each thing you want to store, you will need to malloc()
or use strdup()
then assign it to the pointer in your struct.
You could define :
typedef struct What_if
char *price;
} what_if;
in your struct. The initialize it when you know the exact size of chars to be stored:
struct what_if w;
w.price = malloc (sizeof(char) * NEEDED_CHAR);
You need to allocate what_if_var
with a call to malloc()
before you can assign to it. I would also advise you to avoid using a global variable. Instead make what_if_var
local.
精彩评论