How to pass a pointer to a dynamic array to a void function?
Updated question to reflect answers and my research
Thanks casablanca and wallyk for answering. Success!! I ended up having to edit my strcpy and strcat as well as changing my Displayname(char c_fullname) function and researching some more about the additional errors (both complier and runtime) I was getting, but now I have working code!! Thanks to everyone for your help and patience. My final problem was that I was supposed to write my function declaration and implementation (but not my function call) as "Displayname(char* c_fullname)" not "Displayname(char *c_fullname)". Simple mistake but definitely a costly one as my program was not passing the strings completely. Again thanks to everyne for their help. I know I would have gotten this far without it.
I am posting my completed working code below for anyone else who may have the same issue:
#include <iostream>
#include <cstring>
using namespace std;
void DisplayName(char* c_fullname);
int main()
{
//Declaration of variables
const int n_SIZE = 25; //Size declaration for first, middle, and last name arrays
char c_firstname[n_SIZE]; //Firstname array
char c_middlename[n_SIZE]; //middlename array
char c_lastname[n_SIZE]; //lastname array
int n_namesize = 0; //size declaration for dynamic array
char *c_fullname; //pointer for fullname. Later will be allocated an array size
bool b_test = tr开发者_JS百科ue; //while loop test value
char c_exittest; //test value for user prompt
//Explanation of program to user
cout <<"This program inputs a user's first, middle, and last name.\nNext, the names are dynamically allocated to a single array\nand printed to the screen" <<endl <<endl;
while (b_test == true)
{
cout <<"Please enter a first name: "; //User inputs firstname
cin >>c_firstname;
cout <<"Please enter a middle name: "; //User inputs middlename
cin >>c_middlename;
cout <<"Please enter a last name: "; //User inputs lastname
cin >>c_lastname;
cout <<endl; //endl for aesetics
//Testing of stored variables
/*
cout <<c_firstname;
cout <<c_middlename;
cout <<c_lastname;
cout <<endl <<endl;
cout <<strlen( c_firstname ) <<endl;
cout <<strlen( c_middlename ) <<endl;
cout <<strlen( c_lastname ) <<endl;
*/
//setting size of n_namesize for length of array name[]
n_namesize = (strlen(c_firstname) + strlen(c_middlename ) + strlen( c_lastname ) + 3); //strlen used to determine length of each element + 3 for spaces
c_fullname = new char[n_namesize]; //allocates memory to dynamic array
//Testing of n_namesize
//cout <<n_namesize <<endl <<endl;
//Concatenating strings to array fullname[n_namesize]
strcpy_s(c_fullname, n_namesize, c_firstname ); //copies firstname to fullname
strcat_s(c_fullname, n_namesize, " "); //concatenates space to fullname
strcat_s(c_fullname, n_namesize, c_middlename); //concatenates middlename to fullname
strcat_s(c_fullname, n_namesize, " "); //concatenates space to fullname
strcat_s(c_fullname, n_namesize, c_lastname); //concatenates lastname to fullname
//Testing of fullname[]
//cout <<"Name: "<<c_fullname <<endl <<endl;
//Passing pointer of array to function
DisplayName(c_fullname);
//Prompt users to enter a new name
cout <<"Do you wish to enter a different name?" <<endl;
cout <<"Enter 'y' for yes, or 'n' for no" <<endl;
cin >>c_exittest;
//User wants to enter a new name
if (c_exittest == 'y')
{
delete[] c_fullname; //deallocates memory assigned to array
}
//User does not want to enter a new name
if (c_exittest == 'n')
{
b_test = false; //exits while loop
}
}
}
void DisplayName(char* c_fullname)
{
cout <<"The full name entered is: "<<c_fullname <<endl;
}
Hey everyone,
Thanks for your previous help. I have another assignment (past due) that is giving me problems. I think the assignment is to pass a pointer of a dynamic array to a function which then will display the strings. My instructions is to " pass the constructed array to a function called void DisplayName(char * Name)" However, I keep getting errors. My code is:
#include <iostream>
#include <cstring>
using namespace std;
void DisplayName(char *c_fullname);
int main()
{
//Declaration of variables
const int n_SIZE = 25; //Size declaration for first, middle, and last name arrays
char c_firstname[n_SIZE]; //Firstname array
char c_middlename[n_SIZE]; //middlename array
char c_lastname[n_SIZE]; //lastname array
int n_namesize = 0; //size declaration for dynamic array
char c_fullname[100];
//fullname array. size initialy set to 100
char *c_pointer; //pointer for dynamic array
bool b_test; //while loop test value
char c_exittest; //test value for user prompt
//Explanation of program to user
cout <<"This program inputs a user's first, middle, and last name.\nNext, the names are dynamically allocated to a single array\nand printed to the screen" <<endl <<endl;
while (b_test = true)
{
cout <<"Please enter a first name: "; //User inputs firstname
cin >>c_firstname;
cout <<"Please enter a middle name: "; //User inputs middlename
cin >>c_middlename;
cout <<"Please enter a last name: "; //User inputs lastname
cin >>c_lastname;
cout <<endl; //endl for aesetics
//Testing of stored variables
/*
cout <<c_firstname;
cout <<c_middlename;
cout <<c_lastname;
cout <<endl <<endl;
cout <<strlen( c_firstname ) <<endl;
cout <<strlen( c_middlename ) <<endl;
cout <<strlen( c_lastname ) <<endl;
*/
//setting size of n_namesize for length of array name[]
n_namesize = (strlen(c_firstname) + strlen(c_middlename ) + strlen( c_lastname ) + 3); //strlen used to determine length of each element + 3 for spaces
c_pointer = new char[n_namesize]; //allocates memory to dynamic array
c_pointer = c_fullname; //sets pointer to dynamic array
//Testing of n_namesize
//cout <<n_namesize <<endl;
//Concatenating strings to array fullname[n_namesize]
strcpy_s(c_fullname, c_firstname); //copies firstname to fullname[]
strcat_s(c_fullname, " "); //concatenates space to fullname[]
strcat_s(c_fullname, c_middlename); //concatenates middlename to fullname[]
strcat_s(c_fullname, " "); //concatenates space to fullname[]
strcat_s(c_fullname, c_lastname); //concatenates lastname to fullname[]
//Testing of fullname[]
cout <<"Name: "<<c_fullname <<endl <<endl;
//Passing pointer of array to function
DisplayName(*c_fullname);
//Prompt users to enter a new name
cout <<"Do you wish to enter a different name?" <<endl;
cout <<"Enter 'y' for yes, or 'n' for no" <<endl;
cin >>c_exittest;
//User wants to enter a new name
if (c_exittest == 'y')
{
delete[] &c_fullname; //deallocates memory assigned to array
}
//User does not want to enter a new name
if (c_exittest == 'n')
{
b_test = false; //exits while loop
}
}
}
void DisplayName(char *c_fullname)
{
cout <<"The full name entered is: "<<&c_fullname <<endl;
}
my error is: error C2664: 'DisplayName' : cannot convert parameter 1 from 'char' to 'char *'
I have tried to change several parts to make it work, all of which give me other errors. At this point I am really stuck, so looking for some advice. Thanks in advance for your help,
Kula Dhad
You've got the use of *
and &
mixed up. You don't need any operator to pass a pointer, you just pass it directly. This:
DisplayName(*c_fullname);
should be:
DisplayName(c_fullname);
Again, a char *
is already a string, so you can print it directly. &
takes the address, which is not what you want. This:
cout <<"The full name entered is: "<<&c_fullname <<endl;
should be:
cout <<"The full name entered is: "<<c_fullname <<endl;
Finally, you should NOT delete
memory that wasn't allocated using new
. In your case, c_fullname
was allocated on the stack, so you should remove this line:
delete[] &c_fullname;
Update: Maybe I wasn't clear earlier. The function is still defined to take char *
:
void DisplayName(char *c_fullname);
But you just call it as:
DisplayName(c_fullname);
pass dynamic array to DisplayName(char *) function for displaying and Deallocate dynamically-allocated array
To do this, you just get rid of your existing static array (char c_fullname[n_SIZE]
) and use new
to create the array. In that case, you will also need to use delete
later:
char *c_fullname;
...
c_fullname = new char[n_SIZE];
...
DisplayName(c_fullname);
...
delete [] c_fullname;
To fix, replace
DisplayName(*c_fullname);
with
DisplayName(c_fullname);
c_fullname is already a pointer type. Putting * in front of it dereferences it providing the contents of the element, which is type char
.
By the way, deleting &c_fullname—or anything else associated with c_fullname—will cause nasty runtime problems. There is no need to delete an automatic array.
精彩评论