954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

passing a string to parameter of type char*

i have a header file and one of the function prototypes is

extern "C" LONGBOOL _stdcall LT360LIB_CmdValue(LONGINT Handle, LONGINT CmdID, char * ParamValue);


and there is an example of using this code

z=LT360LIB_CmdValue(0, lt_Goto_CCW, "5.5");


when i try this code it compile and builds correctly but when i run the program , it halts with no error messages ( and the processor goes to 100% as if it went to an infinite loop).

could it be due to passing a string to a character pointer although i had no compiler errors?
Thanks

mike issa
Light Poster
45 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

No.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

so its ok to pass a string to a character pointer and ill have to check for other causes , ill see thanks

mike issa
Light Poster
45 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Well, it does make any difference in assiging the a string to a char pointer. What I really wonder is how is that killing your processor. Is there any loops which goes to an infinite loop? Or there may be some problems with the implemenation of that function may be. What does your application do?

ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

im actually controlling a hardware (turntable) ,
actually i just tested this function, i didnt write any other code ,it could be due to something happening in the .dll it calls. But it occured to me maybe this problem is due to not having the device connected to my pc , the function maybe waiting for a feedback from the device, currently the device is not around me i was just testing the codes.ill check again when i connect the device.

by the way one more question, in the documentation they say this header can be used also to return a string in paramValue

//---------------------------------------------------------------------------
//  Function: 	LT360LIB_CmdValue
//  Abstract:   Sends a Command to the LT360
//              Also dislays error message dialog if command fails
//  Parameters: Handle: LT360 Index returned from Link
//              CmdID : Command Identifier
//              ParamValue : Parameter String for Value, may be nil
//                           <strong>****Get calls return strings in ParamValue****</strong>
//  Returns:    Returns TRUE if executed successfully
//---------------------------------------------------------------------------
extern "C" LONGBOOL _stdcall LT360LIB_CmdValue(LONGINT Handle, LONGINT CmdID, char * <strong>ParamValue</strong>);

by using "lt_GetMoving" instead "lt_Goto_CCW" in the function posted at the beggining. but how can i obtain the string returned in paramvalue, can i just declarea pointer of type e.g char*p, then placing p as the third parameter?

mike issa
Light Poster
45 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

ParamValue must point to a large enough buffer to receive the information. The documentation should provide information wrt 'large enough buffer'.

You might use something like

const int GETMOVING_SIZE 1024;
char paramValue[GETMOVING_SIZE] = {0};

if(LT360LIB_CmdValue(handle, lt_GetMoving, paramValue))
{
    // here you can check the content of paramValue
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

sorry forgot to mention paramvalue is supposed to return (in the lt_GetMoving)
"CCW" or"CW" or "NO"

mike issa
Light Poster
45 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

thanks for the reply, is it possible to check the value of paramvalue after the function call since it is passed by value?

mike issa
Light Poster
45 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

i tried this test code

#include <iostream>
#include <string>
using namespace std;

void test (char *);

void main()
{
const int GETMOVING_SIZE =5;
char paramValue[GETMOVING_SIZE] = {0};
test(paramValue);
cout<<paramValue;

}

void test (char*paramvalue)
{
	paramvalue=new char [5];
	paramvalue="issa";

		
}

but no output came, i think it is due to passing paramvalue by value, which will be same if i tried with the original code since paramvalue is declared as pointer by value(i.e any changes made to paramvalue won't be seen outside the function that used it).
Thanks again

mike issa
Light Poster
45 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

i tried this test code

#include <iostream>
#include <string>
using namespace std;

void test (char *);

void main()
{
const int GETMOVING_SIZE =5;
char paramValue[GETMOVING_SIZE] = {0};
test(paramValue);
cout<<paramValue;

}

void test (char*paramvalue)
{
	paramvalue=new char [5];
	paramvalue="issa";

		
}

but no output came, i think it is due to passing paramvalue by value, which will be same if i tried with the original code since paramvalue is declared as pointer by value(i.e any changes made to paramvalue won't be seen outside the function that used it). Thanks again

Your test() function is making paramvalue point to something entirely different. That's not the way to go about it.

test() is supposed to expect a buffer passed to it, and it ought to fill the buffer with whatever characters it sees fit. It should look more like:

void test(char *paramvalue) {
	strcpy(paramvalue, "issa");
}


Edit:

In my haste, I overlooked this:
paramvalue=new char [5];
paramvalue="issa";

That shows you've got the whole idea of pointers, arrays and strings (and probably a few more things that come into play here) totally jumbled up. You should consult a good book.

ruggedrat
Newbie Poster
3 posts since Jun 2008
Reputation Points: 10
Solved Threads: 2
 

thanks man i got you , i tried it and it works fine. by the way i messed up big time inthe code above some times i used paramvalue and on other occasions paramValue lol thanks again.

anyway just to make sure when i want to take the returned string from ParamValue,

extern "C" LONGBOOL _stdcall LT360LIB_CmdValue(LONGINT Handle, LONGINT CmdID, char * ParamValue);


i'll have to pass an array of type char to the pointer ParamValue and the data will be passed to the array and can be seen outside the function . i.e ( similar to whatmitrmkar said)
thanks

mike issa
Light Poster
45 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You