0

if the given function is as exp:then how to calculate no of rows in the given 2d array input2

void  fun(char *input2[])
{
  //calculate no of rows and in each row no of column 
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Avneet
  • 119
  • 2
  • 12

3 Answers3

4

In general no. In C, it's not possible to calculate the size of an array using only a pointer to it.

However, if you're given a limitation, that the array is terminated by zero(^), then you can count the number of values before the zero using a loop. If it is an array of pointers (such as you have), then the terminator could be a pointer to a specific value. For example, a pointer to zero(^).

Without that limitation, you must store the length in a variable.

(^) you may use any specific value, but zero is a good choice for pointers and characterstrings.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

In C, array parameters decay into simple pointers, so you have to design your function to also accept the length of the array:

void  fun(char *input2[], int input2Len);

If you also define an array length macro you can call fun like this:

#define LEN(arr) ((int) (sizeof (arr) / sizeof (arr)[0]))

char *strings[10];
...
fun(strings, LEN(strings));
August Karlstrom
  • 10,773
  • 7
  • 38
  • 60
0

it is impossible to know the size of an array passed to function as argument, in form of type function(type array[]), that because array is a pointer to the first element of it. but there is no information about the last element.

to get over this issue. there are many solutions that we can implement. some of them may alter the implementation of the function itself as passing the size of the array in a second parameter. but if we want to keep function definition untouched. leaves to two possibilities:

1) setting the number of elements on the first item;

2) marking the end of the array (usually is an empty item)

it is almost the second one which is most adopted, here is the code to illustrate that:

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

int mySizeOfArray(char *input[]){
    char **p=input; //p point to the first element of array
    while(*p){
        p++;//int crementing the pointer
    }
    return p-input;//return the difference between them
}
/*__________________________________________________________
*/
int main(){
    char *input2[]={"First","Second","Third",/*Last*/NULL};
    printf("Size of input=%d\n",mySizeOfArray(input2));
    return 0;
}
milevyo
  • 2,165
  • 1
  • 13
  • 18
  • @user2079303 no no bug in it. i am dereferencing pointer to pointer to char, to get pointer to char; then test it with NULL. as simple as that – milevyo Oct 20 '15 at 12:36
  • Ah, you're right, I didn't perceive all the levels of pointers correctly. The code is good. – eerorika Oct 20 '15 at 12:42
  • @user2079303 but still i am downvoted :) – milevyo Oct 20 '15 at 12:44
  • Yeah... that vote was cast before I falsely claimed a bug so I couldn't have influenced it. I can't speak for the downvote ninja, but I wouldn't upvote your answer because you only posted code and no explanation. Include a few of your comments in the question to explain that the problem is impossible unless certain requirements are met and when your code is applicable and I'll give you a vote. – eerorika Oct 20 '15 at 12:52