|
-
May 5th, 2001, 03:01 PM
#1
Thread Starter
Lively Member
Problems with variables!
I've been working on a project in C and I have written this short, some what crappy, function to make it a CGI for the web. My problem is that I am having trouble making differently variables "work" together.
Here is my function with comments on the Problem Parts:
-------------------
#include <stdio.h>
#include <string.h>
char *myValue(char *logPw, int LorP)
{
char * mainString; // = (char * ) malloc (100);
char * loc = 0;
int index_numb = 0;
int LenOfMain;
mainString = "Login=Brandon&Paswd=12345"; // &=14:len=26
LenOfMain = len(mainString);
loc = strstr(mainString, "&");
if (loc == NULL)
printf("Warning: Bad Post!\n");
else
{
if (LorP == 1) // get login name
{
index_numb = 7;
do
{ strcat(logPw, mainString[index_numb]); //is this wrong?
index_numb++;
}while (index_numb != loc); //problem with pointer-int compare
printf("\n\nString: %s \n Login: %s\n", mainString, logPw);
}
else // get password
{
index_numb = (7 + loc);
do
{ strcat(logPw, mainString[index_numb]); //is this wrong?
index_numb++;
}while (index_numb != LenOfMain); //problem with pointer-int compare
printf("\n\nString: %s \n Password: %s\n", mainString, logPw);
}
}
return logPw;
}
-
May 8th, 2001, 12:10 PM
#2
Use the atoi(...) function to convert an ascii string to an int. It takes a char*, and spits out an int.
Z.
-
May 8th, 2001, 12:52 PM
#3
Frenzied Member
Code:
char * mainString; // = (char * ) malloc (100);
//make it
char * mainString = new char[100]
//
}while (index_numb != loc); //problem with pointer-int compare
//you are comparing int with char (use atoi)
-
May 8th, 2001, 01:39 PM
#4
Monday Morning Lunatic
It's being assigned with a constant string so that will result in a memory leak
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
-
May 8th, 2001, 02:02 PM
#5
Thread Starter
Lively Member
Parskie... how do I fix it?
I am simi lost. Are you talking about:
char * mainString; // = (char * ) malloc (100);
//make it
char * mainString = new char[100]
Can't I just free the memory for mainString?
I can't think of the function off the top of my head.
Is that what you are talking about?
That is why I originally "//" out my malloc so I wouldn't
have to go through all of that. Will I run into the same
problems with the "= new char[100]"?
You have to excuse me... I've started using C again after 2 years of VB... lets just say I am a tad "rusty".
Thanx,
Brandito
-
May 8th, 2001, 02:50 PM
#6
Monday Morning Lunatic
Okay. Quick refresher then 
In C, you'd use malloc/free. In C++, you use new/delete/delete[] (for arrays). Never mix the two on the same variable. It's okay to use malloc and delete in the same program, but this is NOT okay:
Code:
char *x = new char[10];
free(x);
...because they allocate differently and there's no guarantee they even use the same block of memory.
Code:
char x[10]; // Allocated on stack, automatically lost when current {} block finishes
char *x = new char[10]; // Allocated on heap, has to be specifically deleted
char *x = (char*)malloc(10); // Allocated on possibly a different heap, has to be free()d.
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
|