1

below is my code. i know its a very simple problem but i dont know why this code is not working. i simply want to count number of strings in my array but its always showing me a count 1.

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

int main()
{
    char *str[]={"Bharti","Bharat","Akas"};
    int a=PalindromeLengthPuzzle(str);
    return 0;
}
int PalindromeLengthPuzzle(char* input1[])
{
    //const char *mystr=input1;
    int i;
    int sarray= sizeof(input1)/sizeof(input1[0]);
    printf("%s",input1[0]);
    //int sarray=sizeof(mystr)/sizeof(mystr[0]);
    //int x=sizeof(mystr[0]);
    printf("%d",sarray);
    //printf("%s",input1[1]);
    /*for( i=0;i<sarray;i++)
    {
    printf("%s",input1[i]);
    }*/
    return 0;
}

Output of the above code is Bharti1 and it should be 3 instead of 1. Any help would be grateful.

G.S Abhaypal
  • 362
  • 1
  • 6
  • 17

2 Answers2

2

Change your code to:

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

int main()
{
    char *str[]={"Bharti","Bharat","Akas"};
    int a=PalindromeLengthPuzzle(str, sizeof(str)/sizeof(str[0]));
    return 0;
}
int PalindromeLengthPuzzle(char* input1[], int sarray)
{
    //const char *mystr=input1;
    int i;
    printf("%s",input1[0]);
    //int sarray=sizeof(mystr)/sizeof(mystr[0]);
    //int x=sizeof(mystr[0]);
    printf("%d",sarray);
    //printf("%s",input1[1]);
    /*for( i=0;i<sarray;i++)
    {
    printf("%s",input1[i]);
    }*/
    return 0;
}

Arrays decay into pointers in methods hence you cannot calculate the size inside the function - rather pass the size in function call.

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

When an array passed to a function as an argument it implicitly converted to a pointer to its first element.

Thus these three function declarations are equivalent

int PalindromeLengthPuzzle(char* input1[3]);
int PalindromeLengthPuzzle(char* input1[]);
int PalindromeLengthPuzzle(char** input1);

and declare the same one function.

Within the function the parameter is a local variable of type char **

So statement

int sarray= sizeof(input1)/sizeof(input1[0]);

is equivalent to

int sarray= sizeof( char ** )/sizeof( char * );

and evaluates to 1.

You should either declare a second parameter that will contain the number of elements in the array or us some sentinel value as for example NULL as the last velement of the array that within the functionto determine the number of elements by using this sentinel.

Here are demonstrated the two approaches

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

int PalindromeLengthPuzzle( char* input1[], size_t n )
{
   // ...
}

int main()
{
    char *str[] = { "Bharti", "Bharat", "Akas" };
    int a = PalindromeLengthPuzzle( str , sizeof( str ) / sizeof( *str ) );

    return 0;
}

and

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

int PalindromeLengthPuzzle( char* input1[] )
{
   // ...
}

int main()
{
    char *str[] = { "Bharti", "Bharat", "Akas", NULL };
    int a = PalindromeLengthPuzzle( str );

    return 0;
}

Here is a working example with sentinel

#include <stdio.h>

void PalindromeLengthPuzzle( char* input1[] )
{
    while ( *input1 ) puts( *input1++ );
}

int main(void) 
{
    char *str[] = { "Bharti", "Bharat", "Akas", NULL };

    PalindromeLengthPuzzle( str );

    return 0;
}

The output is

Bharti
Bharat
Akas
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335