Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sizeof array....

Posted on 2000-03-21
3
Medium Priority
?
904 Views
Last Modified: 2011-09-20
can someone pls help me how to figure out the size of the _environ array?

printf("%d", sizeof(*_environ)/sizeof(_environ[0]));

...this obviously doesn't work.... why?
0
Comment
Question by:sa9813
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 4

Expert Comment

by:wylliker
ID: 2643431
The _environ[] array is not a fixed length array. It can change if your program adds an environment string, for example.  In that case it's address may even change in memory.  It is also an array of pointers to character strings.


So if you do something like ...


unsigned short EnvCnt = 0;

while(_environ[EnvCnt])
    ++EnvCnt;

You can know how many environment variables there are.


If you want to know the size of all of the strings then you would do something like ...


unsigned short EnvCnt = 0;
// Not including NULL Terminators
unsigned short EnvTotalStringSize = 0;
// including NULL terminators
unsigned short EnvTotalSize = 0;

while(_environ[EnvCnt])
{
    EnvTotalString += strlen(_environ[EnvCnt]);
    EnvTotalSize += strlen(_environ[EnvCnt])+1;
    ++EnvCnt;
}

// Environment ends with TWO (2) NULL terminators
EnvTotalSize += 2;






0
 
LVL 10

Accepted Solution

by:
rbr earned 120 total points
ID: 2643461
It depends on how this array was created.

int array[1000];

printf ("%d",sizeof(array)/sizeof(array[0]));
will print 1000

int *array;

array = calloc (1000,sizeof(int));
printf ("%d",sizeof(array)/sizeof(array[0]));
will print the wrong result.
0
 

Expert Comment

by:mohitdas
ID: 2643479
The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type (including aggregate types). This keyword returns a value of type size_t.

When applied to a statically dimensioned array, sizeof returns the size of the entire array. The sizeof operator cannot return the size of dynamically allocated arrays or external arrays.

int  array[] = { 1, 2, 3, 4, 5 };
size_t  sizearr = sizeof( array ) / sizeof( array[0] );

above will give the number of elements in the array.

sizeof(*_environ)/sizeof(_environ[0])
as this refers to some external array, it'll definitely not work.  :)
Mohit.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question