C Structures

Is structure a value type or a reference type? why?

Questions by ujvala

Showing Answers 1 - 6 of 6 Answers

baseersd

  • Jan 24th, 2008
 

Hi Ujvala,

I am little bit confused with the question,
Are you asking like
1)Calling the function with structure as a parameter, whether changes  value of structure or not
2)Passing of structure can be done by value or reference?

Hope the following program helps in understanding.

#include<stdio.h>

typedef struct _address
{
 int ID;
 char name[20];       
}address;

void copy_ByValue(address obj)
{
 obj.ID = 200;
 strcpy(obj.name,"copy_ByValue");
     printf("nnDetails of obj1 in Copy_ByValue::");
    printf("nID :: %d",obj.ID);
    printf("nName ::%s",obj.name);     
}
void copy_ByReference(address* obj)
{
 obj->ID = 300;
 strcpy(obj->name,"copy_ByReference");   
 printf("nnDetails of obj1 in copy_ByReference::");
    printf("nID :: %d",obj->ID);
    printf("nName ::%s",obj->name);  
}
int main()
{
    address obj1;
    
    obj1.ID = 100;
    strcpy(obj1.name,"C Structures");
        
    printf("nnDetails of obj1 ::");
    printf("nID :: %d",obj1.ID);
    printf("nName ::%s",obj1.name);

    copy_ByValue(obj1);
    printf("nnDetails of obj1 after Copy_ByValue::");
    printf("nID :: %d",obj1.ID);
    printf("nName ::%s",obj1.name);
    copy_ByReference(&obj1);
    printf("nnDetails of obj1::");
    printf("nID :: %d",obj1.ID);
    printf("nName ::%s",obj1.name);
    
 getch();    
}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions