Click here to Skip to main content
15,919,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello
i trying to get size of array of pointers after passing to a function.
but not getting correctly.
look into my code
when i print size of fruit array of pointer it give correct answer 48
but when i print passing argument ptr2 array of pointers it give 8

What I have tried:

#include <stdio.h>
#include "string.h"
const char *const fruit[]=
{
	[1] ="apple1",
	[5]= "apple2",
	[3] ="apple3",
	[4] ="apple4"	
};


int total_index(const char *const  ptr2[])
{

	int a=0;
	a= sizeof(ptr2);
	printf("size of array of pointers %d\n",a);

	a= sizeof(fruit)
	printf("size of array of pointers %d\n",a);

        return a;
	
}
int main()
{
	printf(" indexes %d",total_index(fruit));
	return 0;
}
Posted
Updated 28-May-20 20:24pm
Comments
KarstenK 29-May-20 6:22am    
one short addition: the size of pointers depends only on compilers settings, because it is only a memory address. The objects or arrays to which it is pointing may differ :-O

With const char *const fruit[], fruit is the name of the whole array. But arrays are passed as pointers for efficiency, so ptr2 is equivalent to const char * const *. Ptr2 is just a pointer to an element of the array. So when you do sizeof(fruit) you're checking the size of the array but when you do sizeof(ptr2) you're checking the size of the pointer.

Good basic, free guide to C with a section on some of the interesting array and pointer stuff:
http://cslibrary.stanford.edu/101/EssentialC.pdf[^]

To answer the question in the title: you can't without passing in another parameter (the size of the array) or defining some "termination character" like '\0' to indicate the end of the array. This is precisely why C-style strings end with an '\0'.
 
Share this answer
 
v2
Comments
Vampire 1996 29-May-20 2:00am    
thanks for reply
Actually my program to find the index of a string from array of pointers.
to do that i have to make a loop, to compare every indexed string of array to string.
to end this loop i have to calculate total index in array of pointers.please see blow program

so how can i calculate total indexes in array of pointers using ptr2 pointer


#include <stdio.h>
#include "string.h"
const char *const fruit[]=
{
[1] ="apple1",
[5]= "apple2",
[3] ="apple3",
[4] ="apple4"
};


int findtarget( char* ptr1,const char *const ptr2[])
{

int a=0;
a= sizeof(fruit)/sizeof(fruit[0]);
printf("size of array of pointers %d\n",a);
a= sizeof(fruit)/sizeof(fruit[0]);
printf("size of array of pointers %d\n",a);
int i=0;
for(i=0;i
Jon McKee 29-May-20 2:18am    
If I understand what you're asking and you're forced to use the given code, you can't do it with just ptr2. You'd have to either:
1) Define some termination value for the array (ex. always have an extra value at the end of the array that's equal to null). This would let you iterate through the array until you hit null (end of the array).
2) Add another parameter to findtarget that's the size of the array.
3) Reference the name of the whole array from the function like what you're doing in the code above (sizeof(fruit)/sizeof(fruit[0])).
You can't: C will not let you do that.
An array doesn't really exist - it's "syntactic sugar" for a pointer plus some simple arithmetic, and once the array is passed to the function, it ceases to be an array and becomes "just the pointer" as the system has no idea how many elements it contains.

What you need to do is pass the array and it's element count to your function:
C++
void MyFunc(int arr[], int elements)
   {
   ...
   }
Or
C++
void MyFunc(int *arr, int elements)
   {
   ...
   }
Are equivalent.
 
Share this answer
 
Since your array contains 'holes', the only viable approach is to pass its size, e.g.
C
#include <stdio.h>
#include "string.h"
const char *const fruit[]=
{
        [1] ="apple1",
        [5]= "apple2",
        [3] ="apple3",
        [4] ="apple4"
};


void show(const char *const  item[], size_t items)
{
        for ( size_t n = 0; n < items; ++n)
                printf("item[%lu] = %s\n", n, item[n]);
}
int main()
{
        show( fruit, sizeof(fruit)/sizeof(fruit[0]) );
}


With a more 'regular' array (that is without holes), the 'sentinel' approach is an alternative
C
#include <stdio.h>
#include "string.h"
const char *const fruit[]=
{
        "apple1",
        "apple2",
        "apple3",
        "apple4",
        NULL // sentinel
};


void show(const char *const  item[])
{
        for ( size_t n = 0; item[n]; ++n)
                printf("item[%lu] = %s\n", n, item[n]);
}
int main()
{
        show( fruit );
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900