By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,804 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,804 IT Pros & Developers. It's quick & easy.

[Question] parameter passing between C and C++

P: n/a
Hi, everyone!

We are developing an application using both C and C++.
We have defined a structure in a C program as follows:

typedef struct node {
struct node *next;
int value;
}List;

And we construct a linked list using the above structure.
Then we pass the linked list to a function which is defined
in a C++ program enclosed with "extern "C" { }".

e.g.)
IN C-program

typedef struct node {
struct node *next;
int value;
}List;

int main(){
List *l;

/* l is initialized */

function_in_cpp(l);
}

IN C++-program

extern "C" {
typedef struct node {
struct node *next;
int value;
}List;

function_in_cpp(List *l)
{
....
}
}

When the length of a linked list is 1, the function call to
a function in a C++ is successful. However, when we call the function
in C++ program with the linked list whose length is 2, the actual
parameter in the C++ program is NULL. So, we can't pass the desired
value to a function in the C++ program.

Is there any difference between C and C++ in representing a "struct"
type variable? If any, the difference results in the consequence?

Please anybody help me.

Thanks in advance
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Seung-Uk Oh" <su**@macroimpact.com> wrote...
We are developing an application using both C and C++.
We have defined a structure in a C program as follows:

typedef struct node {
struct node *next;
int value;
}List;

And we construct a linked list using the above structure.
Then we pass the linked list to a function which is defined
in a C++ program enclosed with "extern "C" { }".
That may not work. C object model differs from C++ object
model. Objects created in C may not be recognised in C++.

e.g.)
IN C-program

typedef struct node {
struct node *next;
int value;
}List;

int main(){
List *l;

/* l is initialized */

function_in_cpp(l);
}

IN C++-program

extern "C" {
typedef struct node {
struct node *next;
int value;
}List;

function_in_cpp(List *l)
{
...
}
}

When the length of a linked list is 1, the function call to
a function in a C++ is successful. However, when we call the function
in C++ program with the linked list whose length is 2, the actual
parameter in the C++ program is NULL. So, we can't pass the desired
value to a function in the C++ program.
To verify that claim we would need a complete program. There is
no guarantee that at the moment of the call to the C++ function
with a list of length 2 you haven't make a mistake and the list
isn't really garbage.

Is there any difference between C and C++ in representing a "struct"
type variable? If any, the difference results in the consequence?


There probably is.

Objects created in the part of the program written in a certain
language should be processed in the part of the program written
in the same language, unless there is a way to convert them (like
XML document, for example, but that's very similar to basic I/O)
or they are passed by value interpreted the same way in both
languages (e.g. 'int' or 'char' or pointers thereof).

Victor
Jul 19 '05 #2

P: n/a

Victor Bazarov wrote:

"Seung-Uk Oh" <su**@macroimpact.com> wrote...
We are developing an application using both C and C++.
We have defined a structure in a C program as follows:

typedef struct node {
struct node *next;
int value;
}List;

And we construct a linked list using the above structure.
Then we pass the linked list to a function which is defined
in a C++ program enclosed with "extern "C" { }".


That may not work. C object model differs from C++ object
model. Objects created in C may not be recognised in C++.


Friday.

regards,
alexander.
Jul 19 '05 #3

P: n/a
In article <vf************@corp.supernews.com>, v.********@attAbi.com
says...
"Seung-Uk Oh" <su**@macroimpact.com> wrote...
We are developing an application using both C and C++.
We have defined a structure in a C program as follows:

typedef struct node {
struct node *next;
int value;
}List;

And we construct a linked list using the above structure.
Then we pass the linked list to a function which is defined
in a C++ program enclosed with "extern "C" { }".
That may not work. C object model differs from C++ object
model. Objects created in C may not be recognised in C++.


Hmm...while it's difficult for the C++ standard to require it directly,
the struct above is pretty clearly a POD struct, and it does its best to
ensure that POD structs will be layout compatible with C.

[ ... ]
Is there any difference between C and C++ in representing a "struct"
type variable? If any, the difference results in the consequence?


There probably is.


There shouldn't normally be. As I said above, it's virtually impossible
for the C++ standard to come out and directly say it has to be
compatible with C, it comes about as close as the committee figured they
could to doing exactly that.
Objects created in the part of the program written in a certain
language should be processed in the part of the program written
in the same language, unless there is a way to convert them (like
XML document, for example, but that's very similar to basic I/O)
or they are passed by value interpreted the same way in both
languages (e.g. 'int' or 'char' or pointers thereof).


This can certainly simplify things considerably. Perhaps more to the
point, there's not often much need to mix C and C++ for the simple
reason that at least if you have well-written source code for the C
part, chances are that converting it to compile as C++ will be fairly
trivial. Nonetheless, you don't always have well-written source code to
work with, and in that case, mixing C and C++ is perfectly reasonable
and passing structs back and forth between the two normally works quite
well.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.