473,827 Members | 3,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to print size of char pointer array?

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

Thanks in advance
Jan 25 '08 #1
6 35966
Savage
1,764 Recognized Expert Top Contributor
Hi
Could any one tell me how to print size of char pointer array?
for example
char *ptrArray=new[25];
cout<<sizeof(pt rArray);
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,dat a 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 New Member
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
scoobydoo666
10 New Member
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(ptrArra y, sourceArray, 24); //24: array size hard coded
ptrArray[24]='\0';

I want to code
int arrSize=sizeof( ptrArray);
strncpy(ptrArra y, 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 Recognized Expert Top Contributor
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(ptrArra y, sourceArray, 24); //24: array size hard coded
ptrArray[24]='\0';

I want to code
int arrSize=sizeof( ptrArray);
strncpy(ptrArra y, 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
scoobydoo666
10 New Member
Hi
I solved the problem by using constant int to define the array size. Thanks everyone for your reply.
Jan 28 '08 #6
rjsherman1
1 New Member
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
16462
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 unsigned char ; It has only one pointer contains 1,000 bytes. How can I do this to create pointer list like below. unsigned char** B = new (unsigned char*) ; // Pointer List contains
9
10693
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
8055
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 is the purpose of using a void pointer ? Instead of declaring a pointer variable "void *", can I declare it as "char *" and then later on typcast it to whatever type
11
1559
by: termin | last post by:
consider this char *arr; why doesn't arr=malloc(20); this work ?
1
9679
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 declare in C# side. For eg: in c++ X.dll the function is: extern "C" __declspec(dllexport) void Getcharpointer(unsigned char* v) { //Just for test purpose, i alter and check on the C# side whether it
2
22617
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 read (the file will be written by only this program); file can be either in text or binary (preferably binary as the files may be read repeatedly); the amount and size of strings in the array won't be known until run time (in the example I have it in...
1
2169
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 while retains the others (mostly last and first line in the array). I guess there is some allocation problem but don't know where exactly. Please suggest. int readlines(char *lineptr, int maxlines) { int len, nlines,i; char *p,...
5
4580
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 eloquent r u" char *p2; p2=str;
20
7110
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
9772
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9632
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10746
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10184
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9292
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7731
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4406
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3948
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3067
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.