2

I'm trying to improve my knowledge with pointers by making an pointer who points to another pointer that is practically a string.

Now I want to get size who normally I could get fromsizeof(foo[0])/sizeof(foo[0][0])

Pointer form

char** foo;

4 Answers4

1

You will never get the size of a block of memory where a pointer points to... because there can be anything.

test simply points to a place in memory where some other pointers are stored (to the first one). Each pointer will again lead to another place in Memory where some character values are stored. So, your test variable contains a simple number (the index of a place in Memory) and depending on your operating System sizeof(test) will maybe have 4 bytes or 8 bytes as result regardless of the size of the allocated memory.

sizeof() will work as you might have expected when using stack arrays. If test is declared as

char test[10][20];

Then sizeof(test) will in fact return 200.

Loamsiada
  • 444
  • 3
  • 11
1

If you are allocating something large you use malloc() and malloc receives one argument - the size in bytes(e.g malloc(sizeof(int)*20). malloc also returns a void pointer to the allocated memory. You typically cast this pointer to fit your type. In other words you can't really get the size. You must store it somewhere and pass it to other functions when its needed.

A pointer to pointer (**) is like adding one additional dimension.

[] these are more of a syntax sugar for pointer arithmetic. a[i] would be the same as *(a+i).

This may vary on your system but sizof() will give you these values for these types.

int a;      //4
int b[5];   //20
int* c;     //8
int d[5][5];//100
int** e;    //8
Petar Velev
  • 2,305
  • 12
  • 24
  • Thank you for answering, it seems help full, but it may my English be bad? Or it is to hard, but I never could what does malloc and whats it's role."the size in byte" ok, but what does with these? " also returns a void pointer to the allocated memory", ok I got it, it return a pointer in memory similar to new ... but I'm still confused. Can you give me a more exemplefied comment here? – Laceanu George Jun 28 '17 at 10:56
  • btw, I'm sorry for incomplete reply, I pressed enter by mystake – Laceanu George Jun 28 '17 at 10:59
  • `malloc` is used for dynamic memory allocation. To understand it you must first know how the program uses memory. There is a `stack` and a `heap` `int[5] a;` will be placed on the stack. If you use `int* a = (int*)malloc(sizeof(int)*5)` this will be allocated on the heap. In c++ you use `new` which allocates this memory. Both arrays now can be iterated from 0 to 4 like `a[0]` but the first is on the stack and the second one is on the heap. One more thing is that you can only make stack array when you know the size at compile time e.g using constant to create it. – Petar Velev Jun 28 '17 at 11:00
  • Thank you!!! But likely a string who has null termination and helps strlen to decide the size, I may do the same with the "rows". If I use malloc I do need to do the classic new char/int/etc...? – Laceanu George Jun 28 '17 at 11:06
  • @LaceanuGeorge unfortunately there is no null terminator for normal arrays. You must use a variable for keeping the size and if you are writing function which will receive a pointer to elements you must also pass the size to the function so you can know when to stop. This is not like that in C++ tho. Containers there are a bit more complex and they know their size. For a `std::vector` you have a method `.size()` which will give you the amount of elements in the vector but you also have `.capacity()` which will give you the size of the allocated memory devided by the size of one element. – Petar Velev Jun 28 '17 at 11:19
  • I understand now! I belive vector is a better deal. – Laceanu George Jun 28 '17 at 11:20
  • If you need to know the size, Yes. A vector is like an array who could resize at runtime. You can use a normal array with `std::array` which will also have `.size()` but won't resize. Unfortunately there are no `std::vector` in C it is a C++ container. – Petar Velev Jun 28 '17 at 11:24
1

How I can get it's length (=rows)?

You cannot. Read more in How to get the length of dynamically allocated two dimensional arrays in C


Your attempt:

char** foo;
sizeof(foo[0])/sizeof(foo[0][0])

most probably results in 8, right? That's because you are getting the size of a pointer (which is probably 8 in your system) and then divide by the size of a character, which is always 1.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

sizeof(test)/sizeof(*test) doesn't indicate the number of elements anymore with your declaration, because the compiler doesn't know what is the pointer pointing to, because sizeof() is a compile time operation and hence not dynamic. To find no of elements, you can add a sentinel value:

char **test = {"New York", "Paris", "Cairo", NULL};
int testLen = -1;
while(test[++testLen] != NULL){
//DO NOTHING
}
Mosaaleb
  • 1,028
  • 1
  • 9
  • 23
  • Thank you everyone, your answers were good aswell, and helpful for future use, I'm so sorry because I couldn't pick you, but this was what I was looking for. – Laceanu George Jul 09 '17 at 19:57