1

Well, I have written this simple program that, from a char array and a keyword, stores in another array the part of the string that begins with that keyword.

It works if i don't use any function to manipulate the array, but what i don't understand is why it doesn't work when it's manipulated through a function, there is no output from printf in this case.

Code that don't work:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void createRequest(char *buffer) {
    strcat(buffer, "GET /vfolder.ghp HTTP/1.1\r\n");
    strcat(buffer, "User-Agent: Mozilla/4.0\r\n");
    strcat(buffer, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
    strcat(buffer, "Conection: Keep-Alive\r\n\r\n");
    strcat(buffer, "name=2&password=3");
}

void getParameters(char *buffer, char *parameters) {
    for (int i = 0; i < sizeof(buffer); i++) {
        if (strncmp(buffer + i, "name=", 5) == 0) { strcpy(parameters, buffer + i); }
    }
}

int main(int argc, char *argv[]) {
    char buffer[250];
    char parameters[250];

    memset(buffer, 0x00, sizeof(buffer));
    memset(parameters, 0x00, sizeof(parameters));

    createRequest(buffer);

    fprintf(stdout, "Your buffer is: \n%s\r\n", buffer);

    getParameters(buffer, parameters);

    printf("The parameters are: \n%s\r\n", parameters);
}

Code that work:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void createRequest(char *buffer) {
    strcat(buffer, "GET /vfolder.ghp HTTP/1.1\r\n");
    strcat(buffer, "User-Agent: Mozilla/4.0\r\n");
    strcat(buffer, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
    strcat(buffer, "Conection: Keep-Alive\r\n\r\n");
    strcat(buffer, "name=2&password=3");
}

int main(int argc, char *argv[]) {
    char buffer[250];
    char parameters[250];

    memset(buffer, 0x00, sizeof(buffer));
    memset(parameters, 0x00, sizeof(parameters));

    createRequest(buffer);

    fprintf(stdout, "Your buffer is: \n%s\r\n", buffer);

    for (int i = 0; i < sizeof(buffer); i++) {
        if (strncmp(buffer + i, "name=", 5) == 0) { strcpy(parameters, buffer + i); }
    }

    printf("The parameters are: \n%s\r\n", parameters);
}
2

1 Answer 1

1

You shouldn't use sizeof in getParameters function.

The value of this operation is sizeof(char*), and this is not what you meant.

Change it and use strlen instead:

void getParameters(char *buffer, char *parameters) {
    for (int i = 0; i < strlen(buffer); i++) {
        if (strncmp(buffer + i, "name=", 5) == 0) { strcpy(parameters, buffer + i); }
    }
}

Basically, because it is a char* (which is null-terminated), you can use strlen in order to find its length. For an array of integers, for example, you will have to pass the numbers of elements as an extra parameter.

Sign up to request clarification or add additional context in comments.

5 Comments

To be strictly accurate, they shouldn't have used sizeof in the 'working' code either. It's wrong in both cases, just more drastically wrong in the second case.
@john Absolutely.
It's true i didn't realize that! Thank you very much it is working now, but, in the second case why sizeof is working in main function and is giving me the length of the array instead of giving me the size of char*?
Well, because it is not char* :) It's an array of chars sitting on the stack.
The subject of pointers and arrays in C is not easy to grasp, but it's a crucial step towards understanding C. Once you get hold of it, you begin to unleash the full power of the language. Start here: stackoverflow.com/questions/1335786/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.