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 3 3415
"Seung-Uk Oh" <su**@macroimpa ct.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
Victor Bazarov wrote: "Seung-Uk Oh" <su**@macroimpa ct.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.
In article <vf************ @corp.supernews .com>, v.********@attA bi.com
says... "Seung-Uk Oh" <su**@macroimpa ct.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 database, and I
need the data to be laid out in a tabular format, and set out in a specific
way. At the current point in time, I am copy/pasting the...
|
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 by pointer in C++, is there such way in Python?
Thansk in advance.
J.R.
|
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 a local object when you
pass an object by reference.
But why use a reference? Is there any inherent advantage of using a
reference over using a...
|
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 me with the output of the above code
|
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 ints. The size of the arrays would depend
on some external value (going from 0 to around 1000 max). I would have an
array of max 255 of these...
| |
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: private void myFunction(ref DataSet data)
If I declare it as ref anyway then does it mean it will pass a reference
to another reference similar...
|
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 ?????
OK, this function may return a boolean, and if this is true, then no message
back is really required, but if it fails then some supporting message...
|
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 greatly
appreciated.
Thanks
P.S It this topic is completely unrelated to this news group, I am
sorry.
|
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 little confusion.
***************CODE BEGINS************************************
class MyFrame1(wx.Frame):
def __init__(self, *args, **kwds):
........
|
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. The global
variables and global functions are hidden to prevent from accessing by the
programmers. All global functions share global variables....
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |