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);
}
john
  • 85,011
  • 4
  • 57
  • 81
prosold
  • 13
  • 2
  • No C++ here, so tag removed. – john Apr 25 '20 at 16:21
  • Does this answer your question? [Why isn't the size of an array parameter the same as within main?](https://stackoverflow.com/questions/1975128/why-isnt-the-size-of-an-array-parameter-the-same-as-within-main) – Adrian Mole Apr 25 '20 at 18:32

1 Answers1

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.

MrBens
  • 1,227
  • 1
  • 9
  • 19
  • 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 Apr 25 '20 at 16:20
  • @john Absolutely. – MrBens Apr 25 '20 at 16:22
  • 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*? – prosold Apr 25 '20 at 16:26
  • Well, because it is not char* :) It's an array of chars sitting on the stack. – MrBens Apr 25 '20 at 16:30
  • 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: https://stackoverflow.com/questions/1335786/c-differences-between-char-pointer-and-array – MrBens Apr 25 '20 at 16:41