1

i am receiving a an error and a warning in the following program

void reverse(char ss[],char s2[])
                  ^
1 warning generated

and the error is

warning: sizeof on array function parameter will return size of 'char *' instead of 'char []; [-Wsizeof-array-argument]

printf("%ld \n", sizeof(ss));
                       ^

i am doing a reversing a character array in c

#include<stdio.h>
    #define MAXLENGTH 1000
    void reverse(char ss[],char reversest[]);
    int main()
    {
        char s[MAXLENGTH];
        int c;
        char d[MAXLENGTH];
        int i=0;
        while((c=getchar())!=EOF)
        {
          s[i]=c;
          i++;
        }
        s[i]='\0';
        printf("%ld \n",sizeof(s));
        reverse(s,d);
        printf("%s",d);
        return 0;
    }
    void reverse(char ss[],char s2[])
    {
        int i=0;
        int c=1;
        printf("%ld \n", sizeof(ss));
        for(i=0;ss[i]=='\0';i++)
        {
            for(int j=c-1;j>=0;j--)
            {
                 s2[j]=ss[i];
            }
        }
    }

i am a newbie learning c. i believe i am doing something wrong but i couldn't figure out what it is

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
yash
  • 1,357
  • 2
  • 23
  • 34

1 Answers1

1

Note that on this line:

printf("%ld \n", sizeof(ss));

You are not printing out the number of bytes in the ss array. You are just printing out the size of a pointer. I believe if you fix this line, the warnings will go away.

Maybe you want this instead:

printf("%ld \n", strlen(ss));
bruceg
  • 2,433
  • 1
  • 22
  • 29
  • so the `sizeof` is used to printout the number of bytes? – yash Mar 01 '16 at 18:10
  • 2
    ...also the format should be `printf("%zu \n", sizeof(ss));`.If you are going to use an arbitrary integer format, you should cast `sizeof` – Weather Vane Mar 01 '16 at 18:10
  • the sizeof operator tells you how big the operand is. In this case, we can't tell that ss is an array. It just looks like a pointer. sizeof is a little weird, because when it can tell that the operand is an array and knows how big, it will return the number of bytes in the buffer. – bruceg Mar 01 '16 at 18:12
  • I think you mean "the size of the buffer". `sizeof` has no way of knowing how you have used it. – Weather Vane Mar 01 '16 at 18:13
  • what is `%z` used for ? – yash Mar 01 '16 at 18:13
  • @PaulOgilvie What are you talking about? If he fixes his sizeof problem, the warnings will go away. I gave him one way to fix it. You probably caught my post when I hit submit early. – bruceg Mar 01 '16 at 18:14
  • 1
    As for `%zu` please [see this](http://stackoverflow.com/questions/2524611/how-can-one-print-a-size-t-variable-portably-using-the-printf-family) – Weather Vane Mar 01 '16 at 18:15
  • Also, here's a good link that explains about sizeof and arrays: http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – bruceg Mar 01 '16 at 18:21
  • i replaced it with `strlen` now i am receiving `error: use of undeclared identifier 'strlen'` – yash Mar 01 '16 at 18:22
  • Add #include up at the top of the code after #include – bruceg Mar 01 '16 at 18:26