LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-17-2004, 05:14 AM   #1
alix123
Member
 
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
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 */


}

Last edited by alix123; 11-17-2004 at 05:24 AM.
 
Old 11-17-2004, 05:51 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
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

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:35 AM.

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