1

I've got this simple strstr re-making as an exercise but there's an error I don't understand

#include <iostream>
#include "windows.h"

using namespace std;

int strstr2(char *arr, char *findme)
{
    const int sizearr = sizeof(arr) / sizeof(char);
    const int sizefindme = sizeof(findme) / sizeof(char);

    int j=0;
    for(int i=0; i<sizearr; i++)
    {
        if(arr[i] == findme[j])
        {
            // Match
            if(j == sizefindme-1)
                return i;
            else
                j++;
        }
        else
        {
            j = 0;
        }
    }
    return -1;
}


int main (int argc, char **argv) 
{
    char arr[] = "I'd like 2% milk";
    char toBeFound[] = "like";

    int pos = strstr2(arr, toBeFound);
    Sleep(3000);
}

For some reason the line

const int sizefindme = sizeof(findme) / sizeof(char);

returns sizefindme = 8 while the length of the string "like" is actually 4 (or 5 with the null terminator). And the null terminator is actually present in the toBeFound array. What's wrong with sizeof? I can't spot the error.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108

5 Answers5

8

sizeof(arr) is the size of the pointer, not the size of the array.

You can pass the length of array to the function, or use strlen if you always pass a null-terminated string.

Note: sizeof(char) is always one.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • How does the "strstr" function calculate its strings length? Does it use strlen? – Johnny Pauling Sep 21 '12 at 12:08
  • @Johnny Pauling: I don't know what it exactly does internally (you can check the source of the C library), but you don't need to know the length in advance when you search for the string. – Karoly Horvath Sep 21 '12 at 12:12
  • strstr simply scans until the terminating '\0' character (assuming it doesn't find anything). – christopher Sep 21 '12 at 12:18
4

findme is not an array, it's a char *, and you're on a 64 bit machine so:

sizeof(findme) = 8
sizeof(char)   = 1

You will need to use strlen(findme) instead.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
3

arr and˛findme inside the function strstr2 are pointers to char, not arrays. sizeof is returning the size of that pointer, which happens to be 8 on your platform. Contrary to somewhat common misbelief, arrays are not pointers. You can use strlen instead or pass the size of the arrays to the function by another argument.

Read this great question and answer for more info about arrays.

Community
  • 1
  • 1
jrok
  • 54,456
  • 9
  • 109
  • 141
  • strstr simply scans until the terminating '\0' character (assuming it doesn't find anything). The reason for the weird behavior is the exception to the normal rule that applies in the main function but not in strstr2. An array is implicitly converted into a pointer to it's first element EXCEPT when it is passed to the sizeof operator. You're hitting the exception to the normal rule. In main sizeof(arr) == 17, while sizeof(arr) in strstr2 == 8 (the size of a 64bit pointer). This is because the types differ : in main you're calling sizeof(char[17]), in strstr2 you're calling sizeof(char*). – christopher Sep 21 '12 at 12:29
3

In your example, "findme" isn't an array --- it's a pointer. You're probably on a 64 bit system, so the size of a pointer is 8.

You can't use sizeof() for all sorts of "what's the size of"-questions. If you want the size of a C-string, use strlen(). Or pass the length as an additional parameter, since if you apply sizeof() to the actual array you will get a better result :-)

Christian Stieber
  • 9,954
  • 24
  • 23
2

Why not use strlen? I believe the sizeof(findme) might return the size of the pointer, which would be 8 bytes on a 64 bit machine.

Harel
  • 327
  • 1
  • 5