Finding the size of an array

Started by
5 comments, last by Pseudo_Code 24 years ago
Does anybody know how I could do the following(I have seen people accused of trying to cheat on a school assignment when they post similar posts. But I am in 8th grade and I'm not even open to taking any classes on C/C++. This question is for my own learning purposes and curiosity)
    Get, from the user, a string The string can be unlimited due to dynamic mem allocation using a loop, find out how large the array of chars is.(the array holding the string)
Edited by - Pseudo_Code on 4/10/00 8:23:57 PM
Advertisement
IF it is an array of chars, try strlen
Thanks. I forgot about that. But I have a problem. I have only recently been introduced to dynamic memory allocation. So could somebody give me a hand?

There are two types of strings, as far as the basic parts of the C library functions go. They are: 1) null-terminated, and 2) plain strings.

Null-terminated strings are char arrays that have the last character in the string "punctuated" with a null character, which is '\0' (that's a zero).

Plain strings are just what you think -- no special format. They just store the char's for the string and that's it.

When you "hard-code" a string into a program, C actually creates a null-terminated string. For example:


char mystring[] = "Hello!";



That code tells C to allocate an array of just the right size to hold the string, plus it includes an extra char for the null character. This is how the mystring[] variable really looks in memory: "Hello!\0". That way, all the C library functions don't need to know the size of the array -- they just operate on it until they see a '\0' and then stop. To get the size of a null-terminated string, use the strlen() function. I'm pretty sure that all it takes is the array and returns the length of it.


// Make a string
char mystring[] = "Hello!";

// Store the length in an integer
int string_length = strlen(mystring);

// string_length == 7, and that's with the "invisible" '\0'



However, getting input is a different story. Whatever API or functions you use to get input, you'll have to look up in their docs (it will be well-labeled) and see whether it specifically says it returns a null-terminated string. Otherwise, it is probably a plain string and the function you called to get it probably has a way of giving you the length of the string. Hope that helped...

Oh, about dynamic memory allocation -- you must use pointers to do that. In C, you use malloc() to allocate memory and free() to free it for other apps. In C++, you use new to allocate memory and delete to free it for other apps.

In C:
// create a string pointer -- only points to garbage right now
char* pMemory;

// allocate some memory for that pointer to use
// string_length is the number of char's in the string
pMemory = (char*) malloc(sizeof(char) * string_length);

// pMemory now has some memory to hold your string
// the maximum amount that pMemory can hold is string_length



In C++:
// create a string pointer -- only points to garbage right now
char* pMemory;

// allocate some memory for that pointer to use
// string_length is the number of char's in the string
pMemory = new char[string_length];

// pMemory now has some memory to hold your string
// the maximum amount that pMemory can hold is string_length



When you use pMemory, you use it exactly as if you had allocated it statically, like this: char pMemory[string_length];. To put strings into pMemory, you use a function that takes input from the user, or you can simply copy the bytes into the string, like this:


// put the "Hello!" string into the first available slots
// in this case, that would be slots 0 to 6
strcpy(pMemory, "Hello!", sizeof(char) * strlen("Hello!"));

// put a blank space into the string at the end, which is the eigth char here
// notice I used strcat to move the terminating null so it's still valid
strcat(pMemory, " ");

// add the the "World!" string to the end, which is the 9th char here
// every consecutive time, you must use strcat to move the null to the end
strcat(pMemory, "World!");

// pMemory now contains the string: "Hello! World!"


Just remember that when you're using a function, keep track of where the terminating null is, and you won't have memory access trouble and other things like that.



- null_pointer
Sabre Multimedia


Edited by - null_pointer on 4/11/00 6:07:30 AM
Null pointer:

I think this is wrong

strcpy(pMemory, "Hello!", sizeof(char) * string_length);

should be

strcpy(pMemory, "Hello!", sizeof(char) * strlen("Hello!"));

cause you are copy the memory block from "Hello!" to pMemory and "Hello!" may not be the same length as string_length (ie you are copying out of bounds memory)

..

Why not use the C++ string class? It''s a lot easier than that everlasting null-terminated string from C...

string a("abcd");
int length = a.size();

No need to manually do the allocation/deallocation of memory, since the string object does that for you.

Erik
Void: Thanks, it was my error (it''s corrected now)

Erik Post: I think he just wants to know more about strings how strings really work.


- null_pointer
Sabre Multimedia

This topic is closed to new replies.

Advertisement