LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   hot to set value of pointer to pointer to a structure in C (http://www.linuxquestions.org/questions/programming-9/hot-to-set-value-of-pointer-to-pointer-to-a-structure-in-c-255904/)

alix123 11-17-2004 05:14 AM

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
test{
int no
char * name ;
}

main()
{

struct test ptr;
/*here i do memeset to initialize all the varilble of structureto zero*/

funciton1(&ptr);

}
/*******this is fucntion 1***********?
fucntion1(char *ptr)

{
/*some operations here this fucntion further call the a fucntion2*/
function2(ptr);

}

fucntion2(char *var)

{
/***here is the problem ***/
/*this fucntion i want here to the the values of strcuture
var->no =100;
/*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 */


}

jlliagre 11-17-2004 05:51 AM

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 ...

perfect_circle 11-17-2004 06:40 AM

Code:

test{
int no
char * name ;
}

i don't think this will work.
Try
Code:

typedef struct {
int no;
char * name ;
} test;

then
Code:

main()
{
struct test ptr; <- omit struct, just use test ptr
/* Also ptr in the above line is not a pointer, it's not an error, u may use whatever name you
want, but it's not very good to use the name ptr for something that is not a pointer.*/
 
/*here i do memeset to initialize all the variables of structureto zero*/
or simply do:
ptr.no=0;
ptr.name=NULL;
/* if you are going to set those values later, no need to zero them, or initialize*/

funciton1(&ptr); <- u pass the adress of ptr to function1, that's OK
}

Code:

fucntion1(char *ptr) <- this may work but why cast a struct pointer to a char pointer?????
define fucntion1(test *ptr) better

{
/*some operations here this fucntion further call the a fucntion2*/
function2(ptr);

}

And now your real problem:
Code:

fucntion2(char *var)
{
var->no =100; <- that's OK with me
vat->name?????
}

in the struct you have defined a pointer to point to a character or an array of characters (string);
You haven't allocated any space for the string the pointer points.
You may do that in two ways, static or dynamic.

STATIC
Code:

test{
int no
char  name[100] ; <- allocate space for 100 characters static in compilation
}

the space will be allocated when you do
test ptr; <- for example in main when you define ptr;
and don't do ptr.name=NULL;, you will lose the allocated space
DYNAMIC
Code:

fucntion2(char *var){
var->no =100;
var->name = (char *)malloc(100*sizeof(char)); <- allocate space for 100 characters dynamic in execution
}

then use strcpy or better strncpy to fill the name for both ways
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.

free( var->name);
or free(var.name); if you are in main.

malloc needs stdlib.h (#include <stdlib.h>)
strncpy needs string.h

do
man malloc
man strncpy
for more info


All times are GMT -5. The time now is 11:43 AM.