0

I got a char **function which returns obviously an Array of chars.... the problem is that I don't know how many strings are filled in the array .... char *data[size_of_data]; has been set at the first, but the function may not need all the array fields....

presume I got int size_of_data = 100 , but it just got 15 Strings .... if I want to break after the 15th String, which condition do I need in C ... I got the data already in a char ** field and tried sth like....

 while(strcmp(data[i],'\0'))
{
    msg_send (session, para[0],data[i]);
    printf("------> %s \n",data[i]);
}   
2
  • Do you know what C does to sign the end of any-type array? Commented Aug 23, 2012 at 19:33
  • tell me I thought NULL or'\0' but I am wrong... I m still struggling with c ... coming from Java Commented Aug 23, 2012 at 19:45

4 Answers 4

1

One thing you can do is to also provide the size of the array as an out parameter. Your function signature would look like this

char ** function(size_t * num_strings); //num_strings is an out parameter

Then you do something like so

size_t nstrings = 0;
char **strings = function(&nstrings);
size_t i = 0;
while (i < nstrings) {
    //do stuff
}

There's an advantage to knowing the size, instead of just iterating until you hit a null pointer. Say you prepared an array of 100 char*. What if you use them all? There's no null pointer, and you'll have overrun your buffer. Of course, you could add an extra pointer at the end as a delimiter, but personally I like having the size.

Sign up to request clarification or add additional context in comments.

2 Comments

Out parameters always feel like a hack to me. Even if some standard interfaces use it.
For stylistic reasons, you could also write the function signature as void function(char **arr, size_t *num) . I think this looks a bit nicer, and to my mind the intent is more clear.
1

In your char **function you can put a NULL pointer at the end of your array. So in this case put a NULL after 15th string. Then in your while loop check data[i] == NULL, if its that's the end.

1 Comment

thnks but is there no other way... what I wanted to know is what happens in c at the char *data[15] if I wouldn't put a Null pointer... sth like '\0' or NULL should be automatically set otherwise to which element does the pointer point on the heap at data[15] if not NULL ?
1

The correct way to do this is to have the function place a null pointer (0) into the data array at the first location that is not used.

This can simply be tested by checking to see if the current pointer is 0:

for (i = 0; data[i]; i++) {
    puts(data[i]);
}

Comments

1

Basically there are two common things you can do, either the convention of indicating the end-of-array with a nullpointer or return a struct that contains an additional size_t which holds the number of elements. There is also a variation of the second version using the iterator idiom.

end is null:

char** fun() {
  /* ... */
  char** array = /*...*/;
  unsigned end = 15;
  array[end] = NULL;
  return array;
}

The caller will then stop reading from the result array when it encounters a NULL.

separate size

struct {
  size_t size;
  char** array;
} fun() {
   /* ... */
   char** array = /*...*/;
   unsigned end = 15;
   return {end,array};
}

iterator idiom (lent from C++)

struct {
  char** begin;
  char** end;
} fun() {
   /* ... */
   char** array = /*...*/;
   unsigned end = 15;
   return {array,array+end};
}

This has the advantage that is can be used nicely in loops:

for (struct {char** begin; char** end;} it = fun(); it.begin != it.end; ++it.begin) {
  printf("%s\n",it.begin);
}

And you can also determine the size easily by subtracting end - begin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.