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 9 10661 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." 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. '
Karthik Kumar <ka*******************@yahoo.com> 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 da*******@libero.it wrote: Karthik Kumar <ka*******************@yahoo.com> 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 ? 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?
<OT>
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.
</OT>
--
Karthik. http://akktech.blogspot.com .
'Remove _nospamplz from my email to mail me.'
On 12 Oct 2004 00:37:26 -0700 da*******@libero.it (da*******@libero.it) wrote: Karthik Kumar <ka*******************@yahoo.com> 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. da*******@libero.it wrote: Karthik Kumar <ka*******************@yahoo.com> wrote in message news:<416adc48$1@darkstar>...
As Artie had already suggested, pass the length explicitly 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. da*******@libero.it (da*******@libero.it) wrote in message news:<ed**************************@posting.google. com>... Karthik Kumar <ka*******************@yahoo.com> 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 jo*******@my-deja.com (John Bode) wrote in message news:<43**************************@posting.google. com>... da*******@libero.it (da*******@libero.it) wrote in message news:<ed**************************@posting.google. com>... Karthik Kumar <ka*******************@yahoo.com> 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
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"? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: No Spam |
last post by:
This is probably an easy question, but I have not used C for quite some
time: How do I determine the size of an array?
More specifically, how do I determine the number of incoming parameters?
...
|
by: Jeff |
last post by:
Hello everybody,
I'm kind of new to C programming, but here's a little question.
Usually, when you have an array of chars, you put a \0 at the end of
it to terminate the string. That way, it is...
|
by: terry |
last post by:
Hi,
Could anyone tell me how to determine the size of array of characters
dynamically? For example,
:
:
char *a={"hello","hi","kitty"};
char *b={"orange","apple"};
|
by: arkobose |
last post by:
hey everyone!
i have this little problem. consider the following declaration:
char *array = {"wilson", "string of any size", "etc", "input"};
this is a common data structure used to store...
|
by: junky_fellow |
last post by:
Can the size of pointer variables of different type may be
different on a particular architecture.
For eg.
Can the sizeof (char *) be different from sizeof(int *) or
sizeof (void *) ?
What...
|
by: chellappa |
last post by:
hi Everybody !
decalaring variable in a.h and using that vaaariable in a1.c and
inalization is in main.c it is possible .........pleaase correct that
error
is it Possible? i am trying it...
|
by: chellappa |
last post by:
hi
this simple sorting , but it not running...please correect error for
sorting using pointer or linked list sorting , i did value sorting in
linkedlist
please correct error
#include<stdio.h>...
|
by: arun |
last post by:
suppose i have a pointer to an array of integers.can i initialize each
member of the array using pointers?plz explain
|
by: Chunekit Pong |
last post by:
I have a BYTE array -
BYTE const* pbBinary
I would like to know how many bytes in that byte array
but if I do - sizeof(* pbBinary); - then I got 1
but if I do - sizeof( pbBinary); - then I...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |