473,385 Members | 1,769 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.

How to [share/ transfer] an array [char/int] between C & C++ programs !!

Hello ,

I have two differnt functions in C & in C++ . I want my variable in C++ program which is an array of characters & Integer if possible to be send to my c program. I tried using pointer but the moment control comes out of my C++ i loose all data in that array or i get garbage variables.

Can anyone please guide me for this issue. It will be really great !!
Jan 14 '10 #1
6 3157
newb16
687 512MB
The error is in line 18 of your c++ program. oh wait? Maybe you show the source code first? But it seems that you either deallocate the array too early or pass entirely wrong pointer.
Jan 14 '10 #2
None of above is happening:
1} deallocating the array too early :: Not possible --> as i wait for "recv" function to execute completely to grab all data bytes received from server in my TCP program. And then i assign the [char *] .So eventually the second possible of assigning wrong Pointer is also void. It can not be possible. However i honestly feel i must get the actual progrm & then u can suggest me the exact issue.

Thanks for ur reply though !!! i appreciate it..
Jan 14 '10 #3
johny10151981
1,059 1GB
Newb16 get a good point you missed it. Give the code fragmentation. Then all can see what you did.

There nothing wrong calling a C function from C++ program. All client server application that we develop in C++ have to call the functions of socket.h which is written in C. So you better help us by giving the code fragment that is bothering.

regards,
johny
Jan 14 '10 #4
Expand|Select|Wrap|Line Numbers
  1. // This is the Send function for sending the querry
  2.  
  3. while ( ( nCntSend = send( hClientSocket, szBuffer, nLength, 0 ) != nLength ) )
  4.         {
  5.  
  6.             if ( nCntSend == -1 )
  7.             {
  8.                 cout << "\n Error sending the data to server" << endl ;
  9.                 break ;
  10.             }
  11.             if ( nCntSend == nLength )
  12.                 break ;
  13.  
  14.             //pBuffer += nCntSend ;
  15.             cout << "\n Command Sent to Server is :: " << szBuffer << " "  << endl ;
  16.             nLength -= nCntSend ;
  17.  
  18.         }
  19.  
  20.         Sleep(1000); // wait state time for other CAN3 Device   
  21.                                                    // response
  22.  
  23.  
  24.  
  25. // The following is the Receive Function of which i Need qzBuffer Variable to 
  26. // be transferred to any other c program by pointer/ dll/ or any other method.
  27.  
  28.  
  29. nLength = recv( hClientSocket, qzBuffer, sizeof( qzBuffer ), 0 ) ;
  30.  
  31.         if ( nLength > 0 )
  32.         {
  33.             qzBuffer[ nLength ] = '\0' ;
  34.  
  35.             cout << " Bytes Received from the Server ::  " << nLength << endl ;
  36.                cout << "\n  Response From Server is ::  "<< endl ;
  37.               q =0;
  38.  
  39.            while (q<nLength)
  40.            {
  41.             char b_char = qzBuffer[q];
  42.             tst[q] = b_char;
  43.                q = q++;
  44.            }
----------------------------------------------------------------------------------------------------------------

Is there any way which makes this data transfer between C & C++ variable or else i was planning for writing the data in .txt or .xls file & then sharing it in C program but. Technically its the worst design i can make.

So please have your say upon my issue.

Thnx
Jan 14 '10 #5
johny10151981
1,059 1GB
I have two differnt functions in C & in C++ . I want my variable in C++ program which is an array of characters & Integer if possible to be send to my c program. I tried using pointer but the moment control comes out of my C++ i loose all data in that array or i get garbage variables.

Can anyone please guide me for this issue. It will be really great !!
hi yithape,
Now you have give the source fragmentation. But you post the code wrong didnt followed the convention as well as you didnt mention at which line you have discovered that your data is missing. and what data is missing. please give us the line number as well as also mention the type of qzbuffer.
i mean how did you declared..
char *qzbuffer; //or

char qzbuffer[xxxx];


regards,
johny
Jan 14 '10 #6
johny10151981
1,059 1GB
Hi,
I have asked you the declaration of
qzBuffer
you didnt answer. make sure qzBuffer has memory allocation and do not use
sizeof(qzBuffer) if qzbuffer is a pointer. use literal value.

and make sure that by send() function you are sending correct data to the other end of the net work.

use strcpy or strncpy or any other string copy function it will reduce amount of code.

these are suggestions. you can accept it or not.

regards
johny
Jan 15 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
16
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated,...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
2
by: Icarus27 | last post by:
Hello, I am making a program that runs off of a two dimentional array but I have made it into a game called MouseBot. I am trying to make the game so that you can move the mouse until you get the...
8
by: arnuld | last post by:
i have created a solutions myself. it compiles without any trouble and runs but it prints some strange characters. i am not able to find where is the trouble. ...
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.