19

I am very new in C and was wondering about how to get each element of an array using a pointer. Which is easy if and only if you know the size of the array. So let the code be:

#include <stdio.h>

int main (int argc, string argv[]) {
    char * text = "John Does Nothing";
    char text2[] = "John Does Nothing";

    int s_text = sizeof(text); // returns size of pointer. 8 in 64-bit machine
    int s_text2 = sizeof(text2); //returns 18. the seeked size.

    printf("first string: %s, size: %d\n second string: %s, size: %d\n", text, s_text, text2, s_text2);

    return 0;
}

Now I want to determine the size of text. to do this, I found out, that the String will end with a '\0' character. So I wrote the following function:

int getSize (char * s) {
    char * t; // first copy the pointer to not change the original
    int size = 0;

    for (t = s; s != '\0'; t++) {
        size++;
    }

    return size;
}

This function however does not work as the loop seems to not terminate.

So, is there a way to get the actual size of the chars the pointer points on?

3
  • This unfortunately doesn't change anything in the result. doesn't matter whether I use s != '\0' or *s != '\0' or t != '\0' or *t != '\0', it ends up still not terminating… Commented Jan 21, 2018 at 13:06
  • You're reimplementing strlen. Commented Jan 21, 2018 at 13:07
  • Your function doesn't use s, so what's the point in preserving "the original" value? Commented Jan 21, 2018 at 13:08

2 Answers 2

22

Instead of checking the pointer you have to check the current value. You can do it like this:

int getSize (char * s) {
    char * t; // first copy the pointer to not change the original
    int size = 0;

    for (t = s; *t != '\0'; t++) {
        size++;
    }

    return size;
}

Or more concisely:

int getSize (char * s) {
    char * t;    
    for (t = s; *t != '\0'; t++)
        ;
    return t - s;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Only problem with this one is, it returns always one char less. So adding +1 in the end works. Only if the string isn't empty to begin with. But thanks! Ima work around it now :D
Also, if you are using an empty loop, make it easier to detect by using empty-braces, e.g. for (t = s; *t; t++) {} (you at least moved the ';' to a line of its own -- that was good)
3

There is a typo in this for loop

for (t = s; s != '\0'; t++) {
            ^^^^^^^^^          

I think you mean

for (t = s; *t != '\0'; t++) {
            ^^^^^^^^^          

Nevertheless in general the function does not provide a value that is equivalent to the value returned by the operator sizeof even if you will count also the terminating zero. Instead it provides a value equivalent to the value returned by the standard function strlen.

For example compare the output of this code snippet

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

//...

char s[100] = "Hello christopher westburry";

printf( "sizeof( s ) = %zu\n", sizeof( s ) );
printf( "strlen( s ) = %zu\n", strlen( s ) + 1 );

So your function just calculates the length of a string.

It would be more correctly to define it the following way (using pointers)

size_t getSize ( const char * s ) 
{
    size_t size = 0;

    while ( *s++ ) ++size;

    return size;
}

1 Comment

This should be the accepted answer. size_t getSize ( const char * s ) { size_t size = 0; while ( *s++ ) ++size; return size; } it works even with pointer to any other container with contiguous memory as long as *s is a pointer to the first element and the elements are stored in contiguous memory such that the end of the container produces false.

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.