Results 1 to 6 of 6

Thread: Problems with variables!

  1. #1

    Thread Starter
    Lively Member Brandito's Avatar
    Join Date
    Nov 2000
    Location
    Here, There, Every Where!
    Posts
    106

    Exclamation 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;
    }

  2. #2
    Zaei
    Guest
    Use the atoi(...) function to convert an ascii string to an int. It takes a char*, and spits out an int.

    Z.

  3. #3
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    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)
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  5. #5

    Thread Starter
    Lively Member Brandito's Avatar
    Join Date
    Nov 2000
    Location
    Here, There, Every Where!
    Posts
    106
    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

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
  •  



Click Here to Expand Forum to Full Width