Like Tree1Likes
  • 1 Post By MK27

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

This is a discussion on How to point an address of one struct to another struct variable within the C Programming forums, part of the General Programming Boards category; I want the address of l_temp passed to l, is the syntax i provided right?! Code: struct local_stack_def{ _cc_status cc; ...

  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
    22,303
    No. Recall how to declare a pointer and what is the address-of operator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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;
    KMAN999 likes this.
    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,866
    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,682
    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, 02:31 AM
  3. address of struct variable?
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 06-09-2008, 04: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, 07:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21