473,395 Members | 2,222 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.

how to print size of char pointer array?

Hi
Could any one tell me how to print size of char pointer array?
for example
char *ptrArray=new[25];
cout<<sizeof(ptrArray);
The above code will print 4 (pointer size) instead of 25. how do I print 25?

Thanks in advance
Jan 25 '08 #1
6 35633
Savage
1,764 Expert 1GB
Hi
Could any one tell me how to print size of char pointer array?
for example
char *ptrArray=new[25];
cout<<sizeof(ptrArray);
The above code will print 4 (pointer size) instead of 25. how do I print 25?

Thanks in advance
U can't use sizeof for that.Sizeof returns sizeof variable on program stack memory,and as you know a pointer takes four bytes to be stored on stack.If you allocate dynamically,data that pointer is pointing to is on heap,not on stack.My question is why do you want to do that?If you allocate array dynamically u already know number of elements in it
Jan 25 '08 #2
hsn
237 100+
he means you can't.
when you create an array you give the size of the array.
so you know it already.
there is other variables that you can check their sizes (which are like arrays)
vectors is the very known.
when you create a vector you don't need to give a size for it.
you just keep putting data init. and when you want to check its size you use a function (v.size()).
Jan 25 '08 #3
Hi Savage
I'm using strncpy() and strncat() to copy strings into the array. For this I want to know the size of the array and I don't want to hard code the size.
Instead of coding
char *ptrArray=new[25];
strncpy(ptrArray, sourceArray, 24); //24: array size hard coded
ptrArray[24]='\0';

I want to code
int arrSize=sizeof(ptrArray);
strncpy(ptrArray, sourceArray, arrSize-1);
ptrArray[arrSize-1]='\0';

So in future if array size is changed, no code change is required
Jan 28 '08 #4
gpraghuram
1,275 Expert 1GB
Hi Savage
I'm using strncpy() and strncat() to copy strings into the array. For this I want to know the size of the array and I don't want to hard code the size.
Instead of coding
char *ptrArray=new[25];
strncpy(ptrArray, sourceArray, 24); //24: array size hard coded
ptrArray[24]='\0';

I want to code
int arrSize=sizeof(ptrArray);
strncpy(ptrArray, sourceArray, arrSize-1);
ptrArray[arrSize-1]='\0';

So in future if array size is changed, no code change is required

Usually the practise is using #define the size and use the #define variable.
To get the size of the array we shuld use strlen.

Raghuram
Jan 28 '08 #5
Hi
I solved the problem by using constant int to define the array size. Thanks everyone for your reply.
Jan 28 '08 #6
so you want to count the elements in the char pointer ... it's actually pretty easy to do ..

Expand|Select|Wrap|Line Numbers
  1. int size(char *ptr)
  2.      int charCnt = 0;
  3.      while (*str != '\0')
  4.      {
  5.           charCnt++;
  6.           str++;
  7.      }
  8.  
  9.      return charCnt;
  10. }
  11.  
granted the assumption here is that your counting the characters from a cstring -

frankly .. if you are copying string - why not just use the string.c_str() function which turns your string in a const char *pointer - that is the string.
Nov 6 '11 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bryan Parkoff | last post by:
I want to allocate pointer array into memory so pointer array contains ten pointers. It would be 4 bytes per pointer to be total 40 bytes. Looks like below for example. unsigned char* A = new...
9
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
19
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...
11
by: termin | last post by:
consider this char *arr; why doesn't arr=malloc(20); this work ?
1
by: cbachellam | last post by:
Hello, I am trying to call c++ dll function calls from C#. The c++ dll function takes in pointer to unsigned char array. Can anybody help me in what marshalling parameter value that i have to...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
1
by: MyCGal | last post by:
Hi, I have this code I wrote to copy any length lines into array of pointers. The problem is after storing the individual lines into the char pointer array, the dispaly() chops off some lines...
5
by: rajm2019 | last post by:
hi all, i want to know that what is the actual difference b/w the character array & character pointer.then how u will get the addrees of a char array char str="be silent like u" char *p1="be...
20
by: silverburgh.meryl | last post by:
In my code, I have an array of char* pointer which is populated statically: void function1() { char *ppsz_argv2 = { "abc" , "def", "dummy"}; //... }
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...
1
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.