how to set value of pointer to pointer to a structure in C
a sample program iam giving to explian my problem
i have structure say which has two elemnt one is integer and other is a chracter pointer
i define thhe object of structure in main paas the address of objects in fucntion1 then do some operation again from fucnion1 paas the objects to fcuntion 2 .
in fucntion2 i again do some operation and i want to set the value of strcuture elements here is a problem a dummy very small example is given below to explian the problem
iam a new C coder
i have structure say
follow the commets to understand the problem
char * name ;
struct test ptr;
/*here i do memeset to initialize all the varilble of structureto zero*/
/*******this is fucntion 1***********?
/*some operations here this fucntion further call the a fucntion2*/
/***here is the problem ***/
/*this fucntion i want here to the the values of strcuture
/*is the above statement correc to set the vlaue of the no*/what is the correct line of code to set the elements of the structure*/
/*how to set the charcter element of the structure from this similar to */
/or if my fucniton2 is like function2(char**var) then in thta case what will be the line of code to set the elements of tsructure */
I'ts allright to have some typos in a posting, but yours is going too far (at least for me) because there are so much of them.
Please use the spelling assistant available in the posting interface to improve your message readability ...
You haven't allocated any space for the string the pointer points.
You may do that in two ways, static or dynamic.
test ptr; <- for example in main when you define ptr;
and don't do ptr.name=NULL;, you will lose the allocated space
strncpy(var->name,"John",99); I think strncpy does not count '\0', but i'm not sure
if u use malloc, don't forget to free the allocated space when you don't need it any more.
or free(var.name); if you are in main.
malloc needs stdlib.h (#include <stdlib.h>)
strncpy needs string.h
for more info
|All times are GMT -5. The time now is 03:42 AM.|