struct having pointers's object being passed by value to afunction...

R

Rahul

Hi Everyone,

It is possible to pass a structure to a function by value in c just
like how class is being passed to functions by value in c++.
c++ introduces the copy constructor for this very purpose of making
sure deep copy is performed on c++ objects which have pointers
pointing to some memory location.

It is very much possible to have a c structure having a pointer, how
does c solve the problem? making sure deep copy is done?

Thanks in advance!!!
 
I

Ian Collins

Rahul said:
Hi Everyone,

It is possible to pass a structure to a function by value in c just
like how class is being passed to functions by value in c++.
c++ introduces the copy constructor for this very purpose of making
sure deep copy is performed on c++ objects which have pointers
pointing to some memory location.

It is very much possible to have a c structure having a pointer, how
does c solve the problem? making sure deep copy is done?
It doesn't, you get a shallow copy.

Why would you want to pass such a structure by value?
 
R

Richard Heathfield

Rahul said:
Hi Everyone,

It is possible to pass a structure to a function by value in c just
like how class is being passed to functions by value in c++.
c++ introduces the copy constructor for this very purpose of making
sure deep copy is performed on c++ objects which have pointers
pointing to some memory location.

It is very much possible to have a c structure having a pointer, how
does c solve the problem? making sure deep copy is done?

C doesn't solve the problem. C assumes you know how to solve the problem.

(It's not difficult. Step 1: write a "copy constructor" function. Step 2:
call it to create a copy. Step 3: use the copy. Step 4: destroy the copy
when you're done with it.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top