C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 11th, 2003, 07:51 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
Finding size of a character array?

How would I find the size of a dynamically allocated character array? Here is my code so far:

#include <iostream>
using namespace std;
void main()
{
char *test = new char[];
int i = 0;

cout << "Type in name!\n";
cin >> test;
for (i; i<sizeof(*test); i++)
{
cout << test[i];
}
}

The sizeof operator doesn't seem to be working..could I use scanf()? Thanks in advance.

-andy
__________________
hmmm...

Reply With Quote
  #2  
Old May 11th, 2003, 08:40 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Just like with a non-dynamically created array:

strlen() in the <cstring> header file

Reply With Quote
  #3  
Old May 11th, 2003, 08:42 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 6
Send a message via AIM to andy3109
Is there any other way without having to add another header file?

EDIT:: NM..STRLEN IS PART OF IOSTREAM ..Thanks stud.

-andy

Reply With Quote
  #4  
Old May 11th, 2003, 08:52 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
MSDN says it's in the <string.h> header file which is <cstring> under the new standard, so I don't know why you don't need to include <cstring>, but apparently it works with just <iostream> included.

Reply With Quote
  #5  
Old May 12th, 2003, 12:01 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,867 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 2 h 46 m 57 sec
Reputation Power: 480
Re: Finding size of a character array?

Quote:
Originally posted by andy3109
...
for (i; i<sizeof(*test); i++)

...

The sizeof operator doesn't seem to be working..could I use scanf()? Thanks in advance.

-andy


test is a char pointer, so *test is a char. If you output the value of sizeof(*test), it should be 1.

I know you're using C++, but in both C and C++ the length of a character string is returned by strlen(), whose header file is string.h in both C and C++. However, the value it returns does not include the null-terminator.

Reply With Quote
  #6  
Old May 12th, 2003, 02:07 AM
walter76 walter76 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 18 walter76 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The strlen() function only returns the length till the '\0' appears. if you want to know the length of the allocated memory you have to save it in an extra variable. Another solution is to make char array one field larger and inserting a special character at this position. Then you could calculate the size. If the length of your char array is less than 255 you could also save this value in the first character field.

BTW
Code:
char *test = new char[];
does not allocate any memory for the content of the array. it only allocates a char-array pointer.

Here's some code sample. (Hope the compiler get's it, because it's a long time ago since i've coded c/c++):

Code:
// some variables
char *test = null;  // array pointer
int size = 10;        // size of array

// allocate memory for array dynamically
test = new char[size+1];

// solution one: save special character
test[size] = 0xFF;

// alternative: save len in field
test[0] = (char) size;

// find len of array (solution one)
char* ptr = null;
int len = 0;

while((*ptr) != 0xFF) {
  len++; // increase length
  ptr++; // increment pointer
}

// alternative: find len of array (save len in field)
len = (int) test[0];

Reply With Quote
  #7  
Old May 12th, 2003, 02:19 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,589 Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level)Scorpions4ever User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Day 22 h 40 m 58 sec
Reputation Power: 1001
>> If the length of your char array is less than 255 you could also save this value in the first character field.

IIRC, this was the way that Turbo Pascal (versions <= 3.0) stored string variables. You couldn't declare strings greater than 255 chars in length. The programmer could access individual characters via string[1], string[2] etc., but the compiler wouldn't let the programmer access string[0], because this was the internal variable. You could access it by using a pointer though, but it wasn't such a good idea to modify it, for obvious reasons .

The problem with using this scheme with C/C++ is that string[0] is accessible by the programmer and can be easily modified. Also, your common string handling functions, such as strcmp(), strcat(), sprintf() etc. would have to be rewritten to skip over the first character. Either that, or you have to pass pointer to the second array element to strcmp(), sprintf() etc., instead of the first element.

Reply With Quote
  #8  
Old May 13th, 2003, 01:01 AM
walter76 walter76 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 18 walter76 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
>> IIRC, this was the way that Turbo Pascal (versions <= 3.0) stored string variables. You couldn't declare strings greater than 255 chars in length. The programmer could access individual characters via string[1], string[2] etc., but the compiler wouldn't let the programmer access string[0], because this was the internal variable. You could access it by using a pointer though, but it wasn't such a good idea to modify it, for obvious reasons .

Yes, I know. That's were i got the idea from. I thought andy3109 was searching for solutions without using an extra variable. I also thought he is not working with strings (in that case he could also string from the STL).

Reply With Quote
  #9  
Old May 13th, 2003, 09:05 AM
Doctor - A Doctor - A is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 11 Doctor - A User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
sizeof is a compiler calculator, it is not a funcrtion.
sizeof(*test) probably == sizeof(char)
strlen(test) is a standard solution
but char* is strange idea
if you do not whant headache use STL string,
or MFC CString.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Finding size of a character array?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT