473,395 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Get array size by a pointer dynamically

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"};

void main()
{
char **p=a;
:
:

}

I want to get array size of "a" which is 3 by "p". How?

Thanks!
Nov 14 '05 #1
4 2790
terry <le*******@yahoo.com> scribbled the following:
Hi, Could anyone tell me how to determine the size of array of characters
dynamically?


Impossible in ISO standard C.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
Nov 14 '05 #2
Quoth terry on or about 2004-11-14:
Could anyone tell me how to determine the size of array of characters
dynamically? For example,

char *a[]={"hello","hi","kitty"};

I want to get array size of "a" which is 3 by "p". How?


The usual method is to make the last element in the array a `special'
value, such as 0 or -1 or EOF. For example (caveat: untested)

void
main (void)
{
char *x[] = { "foo", "bar", "baz", 0 };
int idx, len;

len = 0;
for (idx = 0; x[idx]; ++idx)
len++;

printf ("Length is `%d'\n", len);
}

-trent
Nov 14 '05 #3
Trent Buck wrote:
Quoth terry on or about 2004-11-14:
Could anyone tell me how to determine the size of array of characters
dynamically? For example,

char *a[]={"hello","hi","kitty"};

I want to get array size of "a" which is 3 by "p". How?
The usual method is to make the last element in the array a `special'
value, such as 0 or -1 or EOF. For example (caveat: untested)


Why do you think this is the "usual" method? The usual method is to pass
the size of the array along with a ponter to the first element. To avoid
counting the elements in the array, you can use sizeof, both on the array
and on its first element (sizeof a/sizeof a[0]), if the initialization of
the array is visible.

However, if the number of elements in "a" is determined by an
initializer, there is nothing dynamic about it.

void
main (void)
This _may_ cause a compiler targeting a hosted environment to optimize away
the whole program, or to put "bam" in x[3].
{
char *x[] = { "foo", "bar", "baz", 0 };
int idx, len;

len = 0;
for (idx = 0; x[idx]; ++idx)
len++;

printf ("Length is `%d'\n", len);
}


Kurt Watzka

Nov 14 '05 #4
On Sun, 14 Nov 2004 00:20:19 -0800, terry wrote:
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"};

void main()
In C main returns int.
{
char **p=a;
:
:

}

I want to get array size of "a" which is 3 by "p". How?


In C a pointer doesn't know anything about the size of an array it is
pointing at. This is fundamental and means that even various standard
library functions that need to know the size of an array require you to
pass that size (e.g. fgets()). So the direct answer to your question to
your question is that you can't.

As others have said there are various techniques to work around this, e.g.
putting a marker in the array data or maintaining a size variable
alongside the pointer. And this is an issue specifically with pointers, if
you have the ARRAY definition available you can get the size off that e.g.
(sizeof a/sizeof *a) will evaluate to 3 for the definition of a above.

Lawrence

Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
2
by: songfire | last post by:
Hi everybody! Just wondering if it is possible to point to variables in the heap. For example, is this okay? int * ptr_to_DAIA; // pointer to dynamically allocated integer array ptr_to_DAIA =...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
7
by: Sam | last post by:
Hello I have a structure called Company. struct Company { char *employee; char *employee_address; }; I want to build an array of this structure but the number of employees will change...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
33
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.