Thread: How to point an address of one struct to another struct variable

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    32

    Question How to point an address of one struct to another struct variable

    I want the address of l_temp passed to l, is the syntax i provided right?!
    Code:
    struct local_stack_def{
    	_cc_status cc;
    	char command_E[30];
    	char DataEntry[200];
    	char  user_id_E[200];
    	char curr_vol_subvol[ZSYS_VAL_LEN_FILENAME];
    	char program_name_DB[ZSYS_VAL_LEN_FILENAME];
    	short ret_attr_count;
    	short ret_values_maxlen;
    	short user_maxlen;
    	char curr_vol_subvol_t;
    	int x, j, val1, val2, val3;
    	short user_curlen, piece_length, fullname_length;
    	short fpointer, error, resultFD,resultFM, resultUGI,resultGIL, resultFR,ret_val_length;
    	union {
    		   long  id_32;
    		   short id_16[2];
    		  } user_id;
    	struct {
    		 short paid;
    		 short prgm_len;
    		 char  prgm_name_E[ZSYS_VAL_LEN_FILENAME];
    		   } ret_vals;		   
    	 struct {
    	   char val[20];
    	} command_DB[2];
    	 struct {
    	   char val4[50];
    	} user_id_DB[2];
    	struct {
    	   short val5[5];
    	} attr_list[2];
    };
    
    struct local_stack_def l_temp;
    struct local_stack_def l = *l_temp;

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No. Recall how to declare a pointer and what is the address-of operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by KMAN999 View Post
    I want the address of l_temp passed to l, is the syntax i provided right?!
    The "passing" of data in programming-speak generally refers to function parameters. Eg, you pass an argument to a function. I think you mean you want the address of l_temp assigned to l. Assignment is setting the value of a variable. So, eg, you might pass data to a function which the function then assigns to a variable.

    To assign the address of l_temp, you need a pointer and the "address of" operator (&):
    Code:
    struct local_stack_def *l = &l_temp;
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    32
    Quote Originally Posted by MK27 View Post
    The "passing" of data in programming-speak generally refers to function parameters. Eg, you pass an argument to a function. I think you mean you want the address of l_temp assigned to l. Assignment is setting the value of a variable. So, eg, you might pass data to a function which the function then assigns to a variable.

    To assign the address of l_temp, you need a pointer and the "address of" operator (&):
    Code:
    struct local_stack_def *l = &l_temp;
    Thank you!

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Always remember '*' will hold the address of an identifier which can be retrieved by &. Something like this

    Code:
    int *p;   ===> So p is prefixed by *, which mean it can hold an address. But currently it dosn’t hold any address
    int a = 10;  ===> 'a' is just an variable of type int.
    p = &a ;  ====> So 'a' is prefixed by '&' which means that your receives the address of 'a' and assigning it to p. So that P is not pointing to where 'a' resides.
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Get struct address from struct "method"
    By gna in forum C Programming
    Replies: 12
    Last Post: 01-16-2010, 05:41 AM
  2. C De-reference + Memory address together in struct?
    By valaris in forum C Programming
    Replies: 6
    Last Post: 06-18-2008, 01:31 AM
  3. address of struct variable?
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 06-09-2008, 03:01 AM
  4. Address of Structure element, and struct malloc
    By HellsChicken in forum C Programming
    Replies: 1
    Last Post: 11-14-2005, 02:41 PM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM