Ok, so i just want to preface this by saying that my primary issue is concerned with the function option7 and the call to option7 in the switch statement case 7 as well as the function option5 and the call to option 5.
******Function option5 starts at 169, Function option7 starts at 205. Calls are at lines 63 and 72 respectively******
This program is supposed to collect random user input and to store it in a c-string and to perform string manipulation through functions.
So this function that i'm having problems with, namely option7, is essentially supposed to change the users input string into a randomly generated string. More details below------>
It's supposed to do a random resizing of the c-string array via dynamic resizing and RNG and to subsequently fill this new resized array with random ascii values converted into characters. I'm having problems with resizing the array dynamically. I don't really know when to use the new and delete operator and how to transfer the data from the old array into the new resized array
Another thing i'm having problems with function option5 which shows the string statistics: number of letters, punctuation, digits and whitespaces
void resizechararray(char*& in, int newsize)
{
char * tmp = new char[newsize];
strcpy(tmp, in); //copy in to tmp, or whatever other processing you want to do here
delete [] in;
in = tmp;
}
or alternately do it with
char* resizechararray(char* in, int newsize)
{
...blah
delete[in];
return tmp; //only change
}
this is a classic example of why c++ string class exists.
if you call this thing with a char array or pointer to literal it will crash or, worse, do something stupid that will make you wish it had crashed. It will ONLY work with dynamic arrays of char.
Much older C and C++ code that used char arrays would have all 3 types of them floating around and calling the wrong function on the wrong variable would break things.
a hint on #5..
unsigned int stats[256] = 0;
for(... the string)
stats[thestring[i]]++;
and you now have a count of whatever you want. so all the spaces is stats[' '] and so on, you can do the math from there to add up which groups you want to know a total for.
there are some isXXX functions that can be used to pick off some of those. They may cover it all, if you are allowed to use them, or you may need to do it yourself. Depends on what you define punctuation/whitespace/etc as, if the defaults work for you.
Ok it worked but i have no idea why it works. By referencing the pointer that implies that you want to save the changes that occur within the function inside the main function. Thats what passing by reference is all about.
but when i pass the pointer without referencing it isn't it technically still being passed by reference since the pointer is pointing to an array, and an array's name is the starting address of the array. That means i can have access to the array outside of int main, and the changes can be saved
I'm having trouble drawing the boundaries between an array name and a pointer.
void foo(type *tp)
{
tp = new type; //this is lost when the function ends. The parameter is unchanged.
vs
void foo(type *&tp)
{
tp = new type; //the parameter is changed.
this is just like an integer:
void foo(int x)
x = 3; // this does not change the calling parameter
and
void foo(int &x)
x = 3 // and this one does change the calling parameter
its the exact same thing, with the exact same syntax, except that pointers have an * that is bothering you because it is unfamiliar. We all went through that, the * is weird looking for a few weeks as you get a handle on it.
pointer without & is trickysey. Let me preach on it.
if you pass in a pointer to a function and modify the data that the pointer is pointing to, that data is changed no matter if it has & or not. You did not change the pointer, you changed the data it points to, and that is critical to understand. You can't change the pointer, without the &, but you can change its data !!!! After a while you will see why, but this may be one of the top one or two concepts in pointer work that is hard to grasp.
array names are much like a pointer, so long as you do not try to change them. They are, in effect, const *'s. You can't use new or delete on them, but in every other major way, they behave as if pointers.
but when i pass the pointer without referencing it isn't it technically still being passed by reference since the pointer is pointing to an array, and an array's name is the starting address of the array. That means i can have access to the array outside of int main, and the changes can be saved
A pointer is a an object that has a value. That value is an address.
When you pass by value, the function will create a copy of the arguments value. Calling the value() above is like creating a pointer variable: int* v = p; You can do with the v whatever you want, and it is distinct from p.
When you pass by reference, you don't make a copy. Whatever you do to the r, happens to the p.
Both v and r are effectively pointers.
What is a pointer? It is an object that stores an address and allows a dereference that leads us to a value stored at that address. Where the pointers point to is inconsequential to the act of passing them to functions.
In the example both functions do receive the address of gaz in a pointer. They could change the gaz. Neither does even attempt.