PDA

Click to See Complete Forum and Search --> : Problems with variables!


Brandito
May 5th, 2001, 03:01 PM
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;
}

Zaei
May 8th, 2001, 12:10 PM
Use the atoi(...) function to convert an ascii string to an int. It takes a char*, and spits out an int.

Z.

Vlatko
May 8th, 2001, 12:52 PM
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)

parksie
May 8th, 2001, 01:39 PM
It's being assigned with a constant string so that will result in a memory leak :)

Brandito
May 8th, 2001, 02:02 PM
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

parksie
May 8th, 2001, 02:50 PM
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:char *x = new char[10];
free(x);...because they allocate differently and there's no guarantee they even use the same block of memory.


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.