|
-
Jul 30th, 2001, 04:02 PM
#1
Thread Starter
Lively Member
Returning char*/char [] from a function
Hey,
I am having a big problem! I have this function I've written and basically I want it to pass me a "string" (note I am using C not C++). I am currently returning the pointer from a character array in the fuction... but that method is severly flawed!
When it returns different values and puts them in different variables all of the variables equal the last value sent to a varaible..
example:
char *cgidata(myQuery, myVar)
}
char myVarValue[30];
...
return(myVarValue);
}
int main()
{
char *myLog;
char *myPass;
...
myLog = cgidata("login=test&pass=nopass", "login");
myPass = cgidata("login=test&pass=nopass", "pass");
...
}
myLog ends up equaling myPass because myPass was called later!
I got around it by using sprinf(charArray, charPointer); but it only put the first 4 characters that were in the pointer into the Array!
What do I need to do!?! Can I return an Array Value not a pointer from my function? If so... what is the syntax...
Thanx!
Brandito
-
Jul 30th, 2001, 07:29 PM
#2
Monday Morning Lunatic
How about this?
Code:
int cgidata(char *query, char *value, char *buf, int len) {
/* copy up to len chars into buf, return actual number written */
return len;
}
int main(void) {
char buf_a[256], buf_b[256];
int count_a, count_b;
count_a = cgidata("login=test&pass=nopass", "login", buf_a, 256);
}
Alternatively, you could pass a pointer to a pointer, and allocate the memory inside the function using malloc. Although you'd need to remember to free() it afterwards.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jul 31st, 2001, 11:14 AM
#3
Thread Starter
Lively Member
Hermm...
That would be good if I wanted the fuction to return an INT.
I am wanting to return a character Array.... possibly by pointer.
This is a nother example:
log = cgigetdata("login=hi&password=ho", "login");
printf("login-> %s\n", log);
pass = cgigetdata("login=hi&password=ho", "password");
printf("password-> %s\n", pass);
outputs:
login-> hi
password-> ho
That is basically what I want... but it messes up when I do this!
log = cgigetdata("login=hi&password=ho", "login");
pass = cgigetdata("login=hi&password=ho", "password");
printf("login-> %s\n", log);
printf("password-> %s\n", pass);
output:
login-> ho
password-> ho
See... that is my problem! I am returning a pointer and I guess those variables are becoming pointers of that pointer instead of holding the pointers value. I tried converting the pointer into an array /w sprintf... but it only copied the first 4 characters that were in the pointer!
-
Jul 31st, 2001, 11:18 AM
#4
Thread Starter
Lively Member
Sorry
I understand what you mean... sorry.
I will try that... that is a good idea.
... just forget all of the blad-d-blah I wrote above.
Thanx!
Brandito
-
Jul 31st, 2001, 11:20 AM
#5
Monday Morning Lunatic
Hehe ok no problem It all falls into place if you read that one tiny little comment that nobody is ever going to see
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Aug 1st, 2001, 12:30 PM
#6
Thread Starter
Lively Member
Didn't work
I couldn't get that to work... I know what the theory was. I pas an Array. The Function makes a pointer to it. When I add data to that pointer it actually ends up putting it into that array that you passed. But for some reason I couldn't get it to work.
I did how ever fix the problem by using my original idea... and after the function call add a:
strcpy(uniqueVar, pointerVar);
That method works fine... for now.
Thanx though...
Brandito
PS: Parskie... is that a Vampire Tomato off of the cartoon Attack of the Killer Tomatoes?
-
Aug 1st, 2001, 01:18 PM
#7
Monday Morning Lunatic
Nope, one of the Face Offs ("Bite Me" )
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|