0
char *foo(char *dest, const char *src) {
    size_t i;                      
    for (i = 0; dest[i] != '\0'; i++);

Here, I'm iterating to get the size of dest. In this case I’d input "hello " into dest, which is a size of 6. When I try to use sizeof(dest) I get 4 as the returned value. I want to be able to get the size of the content that's inside of dest without using this for loop.

char *foo(char *dest, const char *src) {
    while (*dest != '\0') dest++;           /* increment the length of dest's pointer*/

EDIT:: i'd like to take a moment to show that i was able to get around finding the length directly.

This is all part of a strcat program. The requirement was to not use [ ] brackets to access or move around in memory.

char *strcat(char *dest, const char *src) {
    while (*dest != '\0') dest++;           /* increment the length of dest's pointer*/
    while (*src != '\0')                    /* we will be incrementing up through src*/
        *dest++ = *src++;                   /* while this is happening we are appending
                                             * letter by letter onto the variable dest
                                             */
    *(dest++) = ' ';                        /* increment up one in memory and add a space */
    *(dest++) = '\0';                       /* increment up one in memory and add a null
                                             * termination at the end of our variable dest
                                             */
    return dest;                            /* return the final output */
}
tisaconundrum
  • 2,156
  • 2
  • 22
  • 37

5 Answers5

6

With null terminated strings you have to iterate over each character to work out the length. Even if you use the strlen() it will be doing what your loop does.

0

You're looking for strlen(). But note that it's likely implemented with that same loop.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
0

Since the type of dest in your function is char const*, sizeof(dest) is the same as sizeof(char const*), i.e. the size of a pointer. The fact that you get 4 when you use sizeof(dest) indicates that sizeof of a pointer in your platform is 4.

The only way to get the length of the string is to count the characters until you encounter the null character. This is most likely what strlen does too.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

In C, strings are stored as character arrays terminated in a \0.

If you want to get the number of characters in an array, you have to loop over this array, you cannot get around it.

However, you can store the size in a structure,

typedef struct
{ 
  int size;
  char *data;
}String;

Then, you have to make wrapper functions to write to this String, read from this String, and update the data.

This can be useful if you have lots of reads of size and not many updates or writes (or zero writes for constants).

But generally, the for loop is the better solution.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
-1

yeah!! u can get size without using for loop. introduce library and strlen(dest)+1 will be size of your array.cout<< dest; will surely give your array but sizeof(dest) will not give size of array. I am also confused why that happens.