更新了问题以反映答案和我的研究
感谢卡萨布兰卡和沃利克的回答。成功!!我最终必须编辑我的strcpy和strcat以及更改我的Displayname(char c_fullname)函数并研究更多关于我得到的其他错误(包括编译器和运行时),但现在我有工作代码!感谢大家的帮助和耐心。我最后的问题是我应该把我的函数声明和实现(但不是我的函数调用)写成“Displayname(char * c_fullname)”而不是“Displayname(char * c_fullname)”。简单的错误,但绝对是一个昂贵的错误,因为我的程序没有完全传递字符串。再次感谢everyne的帮助。我知道如果没有它,我会走得这么远。
我正在为可能遇到同样问题的其他人发布我已完成的工作代码:
#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 = true; //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;
}
嘿大家,
感谢您以前的帮助。我有另一项任务(过期)给我带来了问题。我认为赋值是将动态数组的指针传递给函数,然后函数将显示字符串。我的指示是“将构造的数组传递给一个名为void DisplayName(char * Name)的函数”但是,我不断收到错误。我的代码是:
#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;
}
我的错误是: 错误C2664:'DisplayName':无法将参数1从'char'转换为'char *'
我试图更改几个部分以使其工作,所有这些都给我带来了其他错误。在这一点上,我真的陷入困境,所以寻找一些建议。 在此先感谢您的帮助,
Kula Dhad
答案 0 :(得分:2)
您已使用*
和&
混合使用。您不需要任何运算符来传递指针,您只需直接传递它。这样:
DisplayName(*c_fullname);
应该是:
DisplayName(c_fullname);
同样,char *
已经是一个字符串,因此您可以直接打印它。 &
获取的地址不是您想要的地址。这样:
cout <<"The full name entered is: "<<&c_fullname <<endl;
应该是:
cout <<"The full name entered is: "<<c_fullname <<endl;
最后,您应该 NOT delete
内存未使用new
分配。在您的情况下,c_fullname
已在堆栈上分配,因此您应删除此行:
delete[] &c_fullname;
更新:也许我之前不清楚。该函数仍然定义为char *
:
void DisplayName(char *c_fullname);
但你只需将其称为:
DisplayName(c_fullname);
将动态数组传递给DisplayName(char *)函数以显示和释放动态分配的数组
为此,您只需删除现有的静态数组(char c_fullname[n_SIZE]
)并使用new
创建数组。在这种情况下,您还需要稍后使用delete
:
char *c_fullname;
...
c_fullname = new char[n_SIZE];
...
DisplayName(c_fullname);
...
delete [] c_fullname;
答案 1 :(得分:0)
要修复,请更换
DisplayName(*c_fullname);
与
DisplayName(c_fullname);
c_fullname已经是指针类型。将*放在它前面取消引用它提供元素的内容,类型为char
。
顺便说一句,删除&amp; c_fullname-或与c_fullname相关的任何其他内容都会导致令人讨厌的运行时问题。无需删除自动数组。