473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[Question] parameter passing between C and C++

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
3 3403
"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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: LG | last post by:
Just have a question with regards to the clipboard, and how to read what other applications (Adobe InDesignCS) place in the clipboard. I am currently in the process of creating a booklet from a...
46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
17
by: pratik | last post by:
i am working a turbo c; the code i typed in is as follows #include<stdio.h> main() { int a=5; printf("%d%d%d",a++,++a,a); } The output of the above code is very interesting. can anyone help...
26
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of...
2
by: Glenn Lerner | last post by:
If I pass a reference type (such as DataSet) to a function, I'm assuming only a reference is passed (not a copy). So there is no need to declare function parameter as ref for those types? Example:...
14
by: Mr Newbie | last post by:
I am often in the situation where I want to act on the result of a function, but a simple boolean is not enough. For example, I may have a function called isAuthorised ( User, Action ) as ?????...
5
by: RKS | last post by:
I am new to Com programming using C++. Can pointers be passed through interface functions. I would like to pass multi dimensional arrays to functions defined in the com interface. Any help is...
2
by: diffuser78 | last post by:
I wrote a small app in wxPython using wxGlade as designer tool. wxGlade brings and writes a lot of code by itself and thats where my confusion started. Its about parameter passing. Here is my...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.