Thread: Where does structure variable store and what does it store

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    54

    Where does structure variable store and what does it store

    I am trying to figure out Where does structure variable store and what does it store using printf statement

    I have written this code

    Code:
     #include<stdio.h>#include<stdlib.h>
    
    
    typedef struct{
    	
    	int var1;
    	int var2;
    	
    } svar;
    
    
    int main ()
    {
    	
    	svar *spvar = malloc(sizeof(svar));
    	spvar -> var1 = 1;
    	spvar -> var2 = 1;
    	
    	printf("spvar  store at memory location -> %p \n", (void*)spvar); 
        printf("spvar -> var1 store at memory location -> %p \n", (void*)spvar -> var1); 
    	printf("spvar -> var2 store at memory location -> %p \n", (void*)spvar -> var2); 
    	
    	printf("spvar = %d \n", (void*)spvar); 
        printf("spvar -> value = %d \n", (void*)spvar -> var1); 
    	printf("spvar -> var2 = %d \n", (void*)spvar -> var2); 
    	
    	
    	return 0;
    }
    spvar store at memory location -> 00C61598
    spvar -> var1 store at memory location -> 00000001
    spvar -> var2 store at memory location -> 00000001
    spvar = 12981656
    spvar -> value = 1
    spvar -> var2 = 1

    I think the two memory location spvar -> var1 and spvar -> var2 are not getting right

    Where does structure variable store and what does it store-memory-value-jpg

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,798
    You're printing values, not addresses.

    Like this:
    printf("spvar -> var1 store at memory location -> %p \n", (void*)&spvar -> var1);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    54
    Quote Originally Posted by Salem View Post
    You're printing values, not addresses.

    Like this:
    printf("spvar -> var1 store at memory location -> %p \n", (void*)&spvar -> var1);
    Code:
    #include<stdio.h>#include<stdlib.h>
    
    
    typedef struct{
    	
    	int var1;
    	int var2;
    	
    } svar;
    
    
    int main ()
    {
    	
    	svar *spvar = malloc(sizeof(svar));
    	spvar -> var1 = 1;
    	spvar -> var2 = 1;
    	
    	printf("spvar  store at memory location -> %p \n", (void*)spvar); 
        printf("spvar -> var1 store at memory location -> %p \n", (void*)&spvar -> var1); 
    	printf("spvar -> var2 store at memory location -> %p \n", (void*)&spvar -> var2); 
    
    
    	printf("spvar = %d \n", (void*)spvar); 
        printf("spvar -> value = %d \n", (void*)spvar -> var1); 
    	printf("spvar -> var2 = %d \n", (void*)spvar -> var2); 
    	
    	
    	return 0;
    }
    spvar store at memory location -> 00C41598
    spvar -> var1 store at memory location -> 00C41598
    spvar -> var2 store at memory location -> 00C4159C
    spvar = 12850584
    spvar -> value = 1
    spvar -> var2 = 1

    I just want to confirm that spvar variable store at memory location 00C41598 and spvar variable hold value 12850584

    Please give confirm if this is correct

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    46
    Quote Originally Posted by Rahul11 View Post
    [CODE]

    spvar store at memory location -> 00C41598
    spvar -> var1 store at memory location -> 00C41598
    spvar -> var2 store at memory location -> 00C4159C
    spvar = 12850584
    spvar -> value = 1
    spvar -> var2 = 1

    I just want to confirm that spvar variable store at memory location 00C41598 and spvar variable hold value 12850584

    Please give confirm if this is correct
    It looks accurate to me based on the fact that var1 is stored in 4bytes of memory.

    0xc4159c - 0x0c41598 = 4

    Four bytes is makes sense as the size of an int.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    888
    0xc41598 = 12850584

  6. #6
    Registered User
    Join Date
    May 2021
    Posts
    54
    I want to modify the value of structure member without modifying actual address. I have written function but It doesn't works as expected

    Code:
     #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct{
        
        int var1;
        int var2;
        
    } svar;
    
    void foo ( svar *spvar , int fvar1, int fvar2 )
    {
        spvar = malloc(sizeof(svar));
        spvar -> var1 = fvar1;
        spvar -> var2 = fvar2;
        
        printf("value %d stoare at memory location %p (spvar->var1) \n", (void*)spvar -> var1, (void*)&spvar -> var1 );      
        printf("value %d store at memory location %p (spvar->var2) \n", (void*)spvar -> var2, (void*)&spvar -> var2);
        
    }
    
    
    int main ()
    {
        svar *spvar = NULL;    
        foo (spvar , 1, 2 );
        printf("\n");    
        foo (spvar , 3, 4);
        printf("\n");    
        foo (spvar , 3, 4);
        return 0;
    }
    value 1 stoare at memory location 00A01598 (spvar->var1)
    value 2 store at memory location 00A0159C (spvar->var2)

    value 3 stoare at memory location 00A015C8 (spvar->var1)
    value 4 store at memory location 00A015CC (spvar->var2)

    value 3 stoare at memory location 00A03328 (spvar->var1)
    value 4 store at memory location 00A0332C (spvar->var2)

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    You are allocating the memory for the struct three times, and throwing away the memory each time returning from foo()! Allocate the memory BEFORE calling foo(), and pass in the ONE instance of the struct object into foo()!

    Check the return value from malloc() to insure the allocation worked correctly!

    You only have to cast the pointer if you are displaying the address using %p, and NEVER cast an int to a (void *)!

    A good up to date book on the C Language would have explained this and more!
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct{
    
        int var1;
        int var2;
    
    } svar;
    
    void foo ( svar *spvar , int fvar1, int fvar2 )
    {
        spvar -> var1 = fvar1;
        spvar -> var2 = fvar2;
    
        // Cast pointers to (void *), but NOT ints
        printf("value %d store at memory location %p (spvar->var1) \n", spvar -> var1, (void*) &spvar -> var1 );
        printf("value %d store at memory location %p (spvar->var2) \n\n", spvar -> var2, (void*) &spvar -> var2);
    }
    
    
    int main ()
    {
        svar *spvar = NULL;
    
        // Allocate the memory BEFORE calling function foo()
        spvar = malloc(sizeof(svar));
        if(spvar == NULL)
        {
           printf("Memory allocation failed!\n");
           return 1;
        }
    
        foo (spvar , 1, 2 );
    
        foo (spvar , 3, 4);
    
        foo (spvar , 3, 4);
    
        free(spvar);  // Free the memory
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How pointer of structure store variable value
    By vajra11 in forum C Programming
    Replies: 9
    Last Post: 02-09-2020, 02:41 PM
  2. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  3. which variable can store words?
    By Hunterofman in forum C++ Programming
    Replies: 8
    Last Post: 04-28-2008, 05:59 PM
  4. store data from ifstream and store in link list
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2006, 08:50 AM
  5. Do you store store one off data arrays in a class?
    By blood.angel in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2002, 12:05 PM

Tags for this Thread