473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

return a char array from a function

3 New Member
Hi all,

Need a little help here.

I am trying to return the MAC of a codec using the following function but cant work out how to convert the Buffer from char[5] to char as the return value.


Error 5 error C2440: 'return' : cannot convert from 'char [5]' to 'char' j:\Indigo Vision\COM\VBserial.dll\IVserial.cpp 194


Its probably obvious but I can seem to convert it


Tim Norton



Expand|Select|Wrap|Line Numbers
  1. char DisplayDeviceInfo( const VBDEVICEINFO *pDeviceInfo )
  2.  
  3. {
  4.     VBMSRV_PLAYBACKINFO pbInfo;
  5.     UINT nValue;
  6.     int  nSize;
  7.     char Buffer[5];
  8.  
  9.      sprintf(Buffer,
  10.         "%02X:%02X:%02X:%02X:%02X:%02X\n",
  11.         pDeviceInfo->macAddress.abMacAddr[ 0 ],
  12.         pDeviceInfo->macAddress.abMacAddr[ 1 ],
  13.         pDeviceInfo->macAddress.abMacAddr[ 2 ],
  14.         pDeviceInfo->macAddress.abMacAddr[ 3 ],
  15.        pDeviceInfo->macAddress.abMacAddr[ 4 ],
  16.         pDeviceInfo->macAddress.abMacAddr[ 5 ]
  17.         );
  18.  
  19.        return Buffer;
  20. }
  21.  
Oct 23 '07 #1
5 2540
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your function is returning a char.

The name Buffer is a char*.

That's because the name of the array is the address of element 0.

You could change your function to return a char* but you would just crash again since you would return the address of a local variable.

The solution here is to have a char** argument and you allocate your Buffer on the heap using the char** argument:

Expand|Select|Wrap|Line Numbers
  1. void DisplayDeviceInfo( const VBDEVICEINFO *pDeviceInfo, char** Buffer )
  2.  
  3. {
  4.     VBMSRV_PLAYBACKINFO pbInfo;
  5.     UINT nValue;
  6.     int  nSize;
  7.  
  8.     delete *Buffer;      //delete the current Buffer
  9.     char* *Buffer = new char[5];  //allocate a new one
  10.  
  11.      sprintf(*Buffer,
  12.         "%02X:%02X:%02X:%02X:%02X:%02X\n",
  13.         pDeviceInfo->macAddress.abMacAddr[ 0 ],
  14.         pDeviceInfo->macAddress.abMacAddr[ 1 ],
  15.         pDeviceInfo->macAddress.abMacAddr[ 2 ],
  16.         pDeviceInfo->macAddress.abMacAddr[ 3 ],
  17.        pDeviceInfo->macAddress.abMacAddr[ 4 ],
  18.         pDeviceInfo->macAddress.abMacAddr[ 5 ]
  19.         );
  20.  
  21. }
  22.  
Oct 23 '07 #2
tnorton
3 New Member
Thsnks for the response.
I will try that but there is something I iver looked mentioning that I think will impact on this.
I am creating a DLL that is returning the value to VB,
how do I get the charr** to output from the DLL?

May other public Functins all look like this
[code]ccp[
int __stdcall VbCleanup1()
bool __stdcall VbCloseDevice1(HANDLE m_hDevice)
bool __stdcall VbAuxChannelConnect1(HANDLE m_hDevice,const char *IPAddressSerial, VBAUXTYPE SerialPort, int Priority, bool Persistant, VBAUXCHANCALLBACK myCallback)

/CODE]
Oct 23 '07 #3
Cucumber
90 New Member
Thsnks for the response.
I will try that but there is something I iver looked mentioning that I think will impact on this.
I am creating a DLL that is returning the value to VB,
how do I get the charr** to output from the DLL?

May other public Functins all look like this
[code]ccp[
int __stdcall VbCleanup1()
bool __stdcall VbCloseDevice1(HANDLE m_hDevice)
bool __stdcall VbAuxChannelConnect1(HANDLE m_hDevice,const char *IPAddressSerial, VBAUXTYPE SerialPort, int Priority, bool Persistant, VBAUXCHANCALLBACK myCallback)

/CODE]
I never created a DLL for visual basic in C, but as far as I know visual basic uses BSTR strings rather than chars.
Oct 23 '07 #4
tnorton
3 New Member
Forgive my ignorence but what does that mean?
Oct 24 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
Forgive my ignorence but what does that mean?
It usually means your dll is a COM object that supports IDispatch and you address is returned in a VARIANT. If you support IDispatch, then your DLL object will appear in the Visual Basci Object Browser.

The BSTR that Cucumber mentioned is a Basic STRing. That is, it is a char array where the length of the array is at the beginning and it may contain any number of C-style strings separated by a \0. At the very end, the BSTR terminator is a \0\0.

You will need to read up on this.
Oct 24 '07 #6

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

Similar topics

9
by: Steve | last post by:
Hello (and a happy new year) I'm quite new to C++ and have to programm something for school and can't get my head around a couple of things, but at the moment this one is the most important for...
7
by: Fernando | last post by:
Hello, I want call, from main program, to a function (with void arguments), what it run the function program and return the data of type char. I have make a example of code, by it don´t run, I...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
16
by: priya | last post by:
Hi all, I am new to this group.I am working in c language.I have dount in pointer? how to retun array of pointer in function? example main()
2
by: cdg | last post by:
Could anyone tell me why a function return with this program is not returning to "main". The program will compile and should be self-explanatory. And any suggestions for improvements would be...
12
by: Aff | last post by:
Brothers , i am facing a problem which is as follow: class poly { private: char name; int capacity; public: poly(char , int );
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
3
by: nick048 | last post by:
Hi to all I have in my program 2 arrays DIM 2: firstArray and secondArray fisrtArray is for example 'ABCD' plus terminator '\0'. I need to create a function in order to perform this in my...
10
by: vignesh4u | last post by:
I am trying to implement the Split function in C ie. if i have a string: char* S="This is a test"; and if i try to split based on space ' ' it should return an array of strings like: ...
6
by: MN | last post by:
I have 2 questions: 1- Why isn't possible to declare a static char array inside function ? the compiler gives this error: storage size of ‘array’ isn’t constant. 2- Why isn't possible to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
tracyyun
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...
0
agi2029
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,...
0
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...
0
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...
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.