1

I want to make a function that create a new string with a specific lenght. This is the code:

char* newString(int lenght){

    char* newstring =(char*)((calloc(lenght, sizeof(char))));
    newstring[lenght] = '\0';
    return newstring;
}

I use it in this way:

char* string = newString(10);

My problem is that when I do:

printf("String lenght  %lu \n",strlen(string));

I get 0 but I don't understand why. I am neophyte with C. Thank for your time

4
  • 1
    Because calloc zero initializes your array, and you never populate it with anything. Also, it's spelled "length" Commented May 20, 2015 at 22:35
  • 1
    ...and you're exceeding your allocated length with t[lenght] = '\0' regardless, thus invoking undefined behavior. Arrays in C are zero-base-indexed. Meaning 0...(lenght-1) are the allowable range of indexes.. Commented May 20, 2015 at 22:36
  • t[length] = '\0'; is an out of bound write. Commented May 20, 2015 at 22:38
  • I do : newstring[lenght-1]='\0' but the problem remains Commented May 20, 2015 at 22:41

4 Answers 4

4

strlen returns the number of characters before the first '\0'. You'll get the same result if you use:

char s[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int len = strlen(s); // len will be zero.
Sign up to request clarification or add additional context in comments.

Comments

1

There's a couple of issues here. The first is that you're terminating t (and I'm not sure what t references actually) with a null-terminator at index "length", which will be one greater than the size of the array. So you're accessing some random blob of memory there. The second is improperly using calloc. I think you want something more like:

char* new_string(int length) {
    char* new_string;
    new_string = malloc(sizeof(char) * length);
    memset(new_string, 0, length);
    new_string[length-1] = '\0';
    return new_string;
}

Strlen in your example returns 0 because it counts the characters before the null terminator, which happens to be 0 in your implementation.

2 Comments

OP's use of calloc is correct. You do the same exact thing, just with more lines of code. Technically, all this function has to do is return calloc(length, sizeof(char));
I know that the calloc function resets memory(in addition to allocate memory), the function memset does the same thing? what change?
0

If length doesn't include the null termination then you should set t[length - 1] to zero, or alternatively calloc with size length + 1.

1 Comment

If posting an answer, maybe consider answering the question question posted: why strlen(string) reports 0.
0

strlen count the number of character in the c string until it see a "\0" (end of string) so as your string is initialized with "\0" its length is actually 0. change this value to another one like.

  memcpy(string, "good things", 10);
  printf("%d", strlen(string));

Comments

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.