-2

I want to count size of my array (how many values/indexes are there). I've found something like this on StackOverflow

char **sentences;

while ((c = fgetc(fp)) != EOF) { 
    ... fill the array...
}

size_t w = sizeof(sentences); // this is wrong! it returns size of pointer (memory)

It gives me "w = 4" but my array has 6 records. I need this for "for loop" to print my results (tried to count every loop in "while", but I got it in different function and I don't know to return 2 values in array [count|array of strings]).

I will be happy for any idea how to print the whole array.

Jax-p
  • 7,225
  • 4
  • 28
  • 58
  • 1
    You must keep track of how much size you allocate. – Spikatrix Mar 01 '16 at 10:11
  • 4
    `sizeof` tells the size of the pointer, how much it takes from the memory, not the array size. Keep a separate variable that counts the lines in your array. – Koshinae Mar 01 '16 at 10:12
  • I am allocating via constant "MAX_SENTENCES" which is giving me the maximal number of sentences in file. Is it wrong? – Jax-p Mar 01 '16 at 10:12
  • 2
    `char **sentences` is not an array, nor is it a 2D array. If you need a 2D array, then use a 2D array, not a pointer-to-pointer. – Lundin Mar 01 '16 at 10:12
  • @Koshinae I got it, but how should I return it? I mean i got "char** function" which "return sentences" – Jax-p Mar 01 '16 at 10:13
  • @JaxCze: As pointed out: sizeof returns the actuall size of what you ask for. Sentences is a pointer (to a pointer) and has the size of 4 bytes. If you like to know the size of what you read, you need to track it in the while loop. – Aslak Berby Mar 01 '16 at 10:17
  • 3
    Anyway there is too little information for anyone to be able to answer. What are you attempting to do? Allocate an array of strings with variable length? Allocate an array of arrays of strings with variable length? – Lundin Mar 01 '16 at 10:17
  • 1
    Eg. add a variable to your function, that is a pointer to an int, and use that. – Koshinae Mar 01 '16 at 10:19
  • _return 2 values_ : `char **func(int *number_of_record){ ... *number_of_record = count; return sentences; }`, caller-side `int n; char **sentences = func(&n);` – BLUEPIXY Mar 01 '16 at 10:24

1 Answers1

1

You can return your actual count like this:

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

char **doSomething(size_t *count) {
  char **sentences;
  // Mock initialization of sentences, please skip :-)
  sentences = (char **)calloc(13, sizeof(char *));
  for (int i = 0; i < 13; i++) {
    sentences[i] = (char *)calloc(8, sizeof(char));
    sprintf(sentences[i], "quux-%d", i); // Just filling in some foobar here.
  }
  // ---------

  *count = 13; // <-- Look here, we're dereferencing the pointer first.
  return sentences;
}

int main(int argc, char **argv) {
  size_t size;
  char **res;

  // Sending the address of size, so the function can write there.
  res = doSomething(&size);

  for(size_t i = 0; i < size; i++)
    printf("res[%lu]: %s\n", i, res[i]); // You should see the filler outputted to stdout.

  return 0;
}
Koshinae
  • 2,240
  • 2
  • 30
  • 40