459,200 Members | 1,607 Online
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

# Array size using pointer

 P: n/a Hi, is it possible to find the dimension of an array using a pointer? main() { int a[10]; f(a); return; } f(int *b) { /*how can I know here the size of b??*/ /*because sizeof(b) is always 4*/ /*and sizeof(b[0]) is always 4*/ ...... } Thank you Dati Nov 14 '05 #1
9 Replies

 P: n/a da*******@libero.it wrote: Hi, is it possible to find the dimension of an array using a pointer? No. main() int main() /* implicit `int' was eliminated in C99 */ { int a[10]; f(a); f(a, sizeof a / sizeof *a); return; } f(int *b) void f(int *b, size_t n) { /*how can I know here the size of b??*/ By passing it as an argument. /*because sizeof(b) is always 4*/ /*and sizeof(b[0]) is always 4*/ ..... } HTH, --ag -- Artie Gold -- Austin, Texas "If you don't think it matters, you're not paying attention." Nov 14 '05 #2

 P: n/a da*******@libero.it wrote: Hi, is it possible to find the dimension of an array using a pointer? main() { int a[10]; f(a); return; } f(int *b) { /*how can I know here the size of b??*/ /*because sizeof(b) is always 4*/ /*and sizeof(b[0]) is always 4*/ Both ought to be the same, since both are pointers. And you are trying to get the size of a pointer variable (which is implementation - dependent). As Artie had already suggested, pass the length explicitly as an argument to the function. -- Karthik. http://akktech.blogspot.com . ' Remove _nospamplz from my email to mail me. ' Nov 14 '05 #3

 P: n/a Karthik Kumar wrote in message news:<416adc48\$1@darkstar>... da*******@libero.it wrote: Hi, is it possible to find the dimension of an array using a pointer? main() { int a[10]; f(a); return; } f(int *b) { /*how can I know here the size of b??*/ /*because sizeof(b) is always 4*/ /*and sizeof(b[0]) is always 4*/ Both ought to be the same, since both are pointers. And you are trying to get the size of a pointer variable (which is implementation - dependent). As Artie had already suggested, pass the length explicitly as an argument to the function. I know, but I can't do it. Do you thing it is possible to find in the stack this information? I mean, I have the position of the first element of the array, then, moving up and down in the stack, I find this number. Or is the stack a simple storage for the program and only it knows this information. And then how does really operate sizeof? Has it an internal table with all variables? Thank you Best Regards Dati Remo Nov 14 '05 #4

 P: n/a da*******@libero.it wrote: Karthik Kumar wrote in message news:<416adc48\$1@darkstar>...da*******@libero.it wrote:Hi, is it possible to find the dimension of an array using a pointer?main(){int a[10];f(a);return;} f(int *b){/*how can I know here the size of b??*//*because sizeof(b) is always 4*//*and sizeof(b[0]) is always 4*/ Both ought to be the same, since both are pointers.And you are trying to get the size of a pointer variable(which is implementation - dependent). As Artie had already suggested, pass the lengthexplicitly as an argument to the function. I know, but I can't do it. Why ? What is the problem ? After all, you are creating the array and you should know its dimensions. Do you thing it is possible to find in the stack this information? I mean, I have the position of the first element of the array, then, moving up and down in the stack, I find this number. How ? You would land up in *undefined behaviour* territory the moment you access beyond the array boundary. Or is the stack a simple storage for the program and only it knows this information. 1. The C standard does not talk of stack / heap (though most of the popular implementations do ). 2. The memory manager does not even come into the picture when you are talking about sizeof. It is the compiler's job to determine its value. And then how does really operate sizeof? Has it an internal table with all variables? In compiler design, it is called *symbol table*. And a given compiler evaluates sizeof at compile-time thanks to the symbol table, it (the compiler system) would have generated. But then, we are discussing about the implementation of C. it outside the scope of the C standard. -- Karthik. http://akktech.blogspot.com . 'Remove _nospamplz from my email to mail me.' Nov 14 '05 #5

 P: n/a On 12 Oct 2004 00:37:26 -0700 da*******@libero.it (da*******@libero.it) wrote: Karthik Kumar wrote in message news:<416adc48\$1@darkstar>... da*******@libero.it wrote: Hi, is it possible to find the dimension of an array using a pointer? main() { int a[10]; f(a); return; } f(int *b) { /*how can I know here the size of b??*/ /*because sizeof(b) is always 4*/ /*and sizeof(b[0]) is always 4*/ Both ought to be the same, since both are pointers. And you are trying to get the size of a pointer variable (which is implementation - dependent). As Artie had already suggested, pass the length explicitly as an argument to the function. I know, but I can't do it. In that case you are in trouble. Do you thing it is possible to find in the stack this information? No. 1) The C programming language does not require the use of a stack. 2) There is no reason for the compiler to store the size of the array on the stack so it is unlikely that it will. 3) As soon as you move a pointer out of an object you have created and deference it (the only way I can think of for you to examine the stack if there is one) you have entered the realms of undefined behaviour and anything could happen. I mean, I have the position of the first element of the array, then, moving up and down in the stack, I find this number. Or is the stack a simple storage for the program and only it knows this information. No. Forget it. Either find a way to pass the size in, find a way of including an "end of array" marker like the 0 used to mark the end of strings, or find another language. And then how does really operate sizeof? Has it an internal table with all variables? sizeof could be implemented in any way the compiler writer chooses. However, IMHO, since the compiler has to know the size of the object in any case (how else can it allocate enough space for it?) the compiler can just look up that size internally and substitute in the appropriate value at compile time. -- Flash Gordon Sometimes I think shooting would be far too good for some people. Although my email address says spam, it is real and I read it. Nov 14 '05 #6

 P: n/a da*******@libero.it wrote: Karthik Kumar wrote in message news:<416adc48\$1@darkstar>... As Artie had already suggested, pass the lengthexplicitly as an argument to the function. I know, but I can't do it. Assuming the reason is that you have control of the implementation but not the interface one thing you can do is to store the size somewhere the function can access it. A file scope variable of suitable linkage should do the trick. This should be a last resort solution as variables with file scope are bad except when they are good... It also smells like a hack, a workaround for architectural limits of the design. If you do not have control of the design you should find out whether these limits are there for a good reason or just because your particular need was not thought when created. If you do _not_ have control of the code that calls the function that needs the size then you are fubared with respect to standard C. Try asking in a group which is dedicated to your compiler perhaps. Otherwise, give some more context and we'll help you if we can. -- Thomas. Nov 14 '05 #7

 P: n/a da*******@libero.it (da*******@libero.it) wrote in message news:... Karthik Kumar wrote in message news:<416adc48\$1@darkstar>... da*******@libero.it wrote: Hi, is it possible to find the dimension of an array using a pointer? main() { int a[10]; f(a); return; } f(int *b) { /*how can I know here the size of b??*/ /*because sizeof(b) is always 4*/ /*and sizeof(b[0]) is always 4*/ Both ought to be the same, since both are pointers. And you are trying to get the size of a pointer variable (which is implementation - dependent). As Artie had already suggested, pass the length explicitly as an argument to the function. I know, but I can't do it. Why not? Do you thing it is possible to find in the stack this information? No. Seriously, there is no (portable) way to find out the physical size of an array based on a pointer to the first element alone. You have two choices: 1. Save the size of the array and either pass it as an argument or save it in a file scope variable. 2. Write a special sentinel value to the last element of the array, like how C strings are arrays of char terminated with a 0. This method is far more error prone, though, and won't necessarily tell you the *physical* size of the array, just how many elements come before the sentinel. Actually, there's a third choice: pass a pointer to the array (which is not the same thing as passing a pointer to the first element): int f(int (*b)[10]) { /* sizeof *b == sizeof (int) * 10 */ } int main (void) { int a[10]; f(&a); return 0; } The only problem is that this assumes you're only working with arrays with 10 elements, so it's not very flexible. I mean, I have the position of the first element of the array, then, moving up and down in the stack, I find this number. Or is the stack a simple storage for the program and only it knows this information. Aside from the fact that not all machines even *use* a stack for passing arguments to functions, what you're passing is a *pointer* type, not an array type. As far as the called function is concerned, b is a pointer to a single int object, not an array. That object may be the first element of an array. It may be the last element of an array. It may be a scalar variable. There is simply no way for the called function to know based on the pointer alone. And then how does really operate sizeof? Has it an internal table with all variables? Possibly. Or it could do something completely different. It's up to the compiler writer to decide how sizeof actually gets implemented. Thank you Best Regards Dati Remo Nov 14 '05 #8

 P: n/a jo*******@my-deja.com (John Bode) wrote in message news:<43**************************@posting.google. com>... da*******@libero.it (da*******@libero.it) wrote in message news:... Karthik Kumar wrote in message news:<416adc48\$1@darkstar>... da*******@libero.it wrote: > Hi, is it possible to find the dimension of an array using a pointer? > > main() > { > int a[10]; > > f(a); > > return; > } > > > f(int *b) > { > /*how can I know here the size of b??*/ > /*because sizeof(b) is always 4*/ > /*and sizeof(b[0]) is always 4*/ Both ought to be the same, since both are pointers. And you are trying to get the size of a pointer variable (which is implementation - dependent). As Artie had already suggested, pass the length explicitly as an argument to the function. I know, but I can't do it. Why not? Do you thing it is possible to find in the stack this information? No. Seriously, there is no (portable) way to find out the physical size of an array based on a pointer to the first element alone. You have two choices: 1. Save the size of the array and either pass it as an argument or save it in a file scope variable. 2. Write a special sentinel value to the last element of the array, like how C strings are arrays of char terminated with a 0. This method is far more error prone, though, and won't necessarily tell you the *physical* size of the array, just how many elements come before the sentinel. Actually, there's a third choice: pass a pointer to the array (which is not the same thing as passing a pointer to the first element): int f(int (*b)[10]) { /* sizeof *b == sizeof (int) * 10 */ } int main (void) { int a[10]; f(&a); return 0; } The only problem is that this assumes you're only working with arrays with 10 elements, so it's not very flexible. I mean, I have the position of the first element of the array, then, moving up and down in the stack, I find this number. Or is the stack a simple storage for the program and only it knows this information. Aside from the fact that not all machines even *use* a stack for passing arguments to functions, what you're passing is a *pointer* type, not an array type. As far as the called function is concerned, b is a pointer to a single int object, not an array. That object may be the first element of an array. It may be the last element of an array. It may be a scalar variable. There is simply no way for the called function to know based on the pointer alone. And then how does really operate sizeof? Has it an internal table with all variables? Possibly. Or it could do something completely different. It's up to the compiler writer to decide how sizeof actually gets implemented. Thank you Best Regards Dati Remo Thanks to all. I'll try other solutions. Dati Remo Nov 14 '05 #9

 P: n/a Groovy hepcat Karthik Kumar was jivin' on Mon, 11 Oct 2004 12:13:11 -0700 in comp.lang.c. Re: Array size using pointer's a cool scene! Dig it! da*******@libero.it wrote: Hi, is it possible to find the dimension of an array using a pointer? main() { int a[10]; f(a); return; This return statement is pointless. } f(int *b) { /*how can I know here the size of b??*/ /*because sizeof(b) is always 4*/ /*and sizeof(b[0]) is always 4*/ Both ought to be the same, since both are pointers. That is incorrect. b is a pointer, but b[0] is an int. And you are trying to get the size of a pointer variable(which is implementation - dependent). No, he is trying to determine the size of an array from a pointer pointing at the array's first element. But, as has been stated time and time again here, it is impossible. -- Dig the even newer still, yet more improved, sig! http://alphalink.com.au/~phaywood/ "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker. I know it's not "technically correct" English; but since when was rock & roll "technically correct"? Nov 14 '05 #10

### This discussion thread is closed

Replies have been disabled for this discussion.