Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-17-2004, 05:14 AM   #1
Registered: Nov 2004
Posts: 63

Rep: Reputation: 15
Angry 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
int no
char * name ;


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


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

/*some operations here this fucntion further call the a fucntion2*/


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 */


Last edited by alix123; 11-17-2004 at 05:24 AM.
Old 11-17-2004, 05:51 AM   #2
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.3, Oracle Linux, Mint
Posts: 9,703

Rep: Reputation: 412Reputation: 412Reputation: 412Reputation: 412Reputation: 412
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 ...
Old 11-17-2004, 06:40 AM   #3
Senior Member
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
int no
char * name ;
i don't think this will work.
typedef struct {
int no;
char * name ;
} test;
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:;;
/* 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 
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*/

And now your real problem:
fucntion2(char *var)
var->no =100; <- that's OK with me 
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.

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;, you will lose the allocated space
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(; if you are in main.

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

man malloc
man strncpy
for more info

Last edited by perfect_circle; 11-17-2004 at 06:55 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
pass pointer,, return pointer??? blizunt7 Programming 3 07-23-2005 01:36 PM
returning data to main() via a pointer to a pointer. slzckboy Programming 3 05-30-2005 01:20 PM
is *- -p = '\n' valid? can i set \n char to a pointer? feetyouwell Programming 1 10-01-2004 01:09 AM
Fedora Core 2 + KDE, can´t set mouse pointer size in .Xdefaults rlinuz Fedora 0 06-20-2004 12:23 PM
pointer to structure Xiangbuilder Programming 2 10-09-2003 03:39 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:12 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration