将指针设置为函数的返回值
Setting pointer to the return value of a function
我正在尝试设置变量 char * vowels and char * consonants 作为函数搜索vowels和searchConsonants的返回值分别。
尽管我测试代码时,上面的变量已正确设置,但不会传递回主。在测试中
cout << "text vowels" << vowels << "sametext" << consonants; ///something like this.
现在不显示辅音值。
这是我的代码,任何建议都会非常有帮助。除了我不能使用字符串。(对于一个班级(
这也是发布代码的适当方法?
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
const int SIZE = 7;
//This function greets the user.
void greetUser();
//This funcion asks for the array of letters.
char * inputLetters(char * inputArray);
//This will capitalize all letters to make them easier for the computer
//to compare.
char * capitalizeLetters(char * inputArray);
//This function will search the array for vowesl. If no vowels no game.
char * searchVowels(char * arrayCopy);
///This function will search the array for consonants.
char * searchConsonants(char * arrayCopy);
//This capitalizes all the letters in the initial array.
char * capitalizeLetters(char * inputArray)
{
for (int i = 0; i < 6; ++i)
{
inputArray[i] = toupper(inputArray[i]);
}
// inputArray = toupper(inputArray);
return inputArray;
}
//This program will search the array for consonants
//and return the consonants array.
char * searchConsonants(char * arrayCopy)
{
char * consonants; consonants = new char[SIZE];
for (int i = 0; i < 6; ++i)
{//I feel like I could make this into a function itself...hmm
if( arrayCopy[i] != 'A' && arrayCopy[i] != 'E'
&& arrayCopy[i] != 'I' && arrayCopy[i] != 'O' &&
arrayCopy[i] != 'U' && arrayCopy[i] != 'Y')
{
consonants[i] = arrayCopy[i];
}
}
return consonants;
}
在方法searchVowels
中,您似乎具有以下代码:
if( arrayCopy[i] == 'A' && arrayCopy[i] == 'E'
&& arrayCopy[i] == 'I' && arrayCopy[i] == 'O' &&
arrayCopy[i] == 'U' && arrayCopy[i] == 'Y')
{
arrVowels[i] = arrayCopy[i];
}
您期望arrayCopy[i]
如何通过支票,因为它不能同时具有所有元音。我认为您正在这里寻找OR
检查。
if( arrayCopy[i] == 'A' || arrayCopy[i] == 'E'
|| arrayCopy[i] == 'I' || arrayCopy[i] == 'O' ||
arrayCopy[i] == 'U' || arrayCopy[i] == 'Y')
{
arrVowels[i] = arrayCopy[i];
}
在上述情况下,如果支票通过,它可能会填充arrayVowels
。
另外,您可以将上述代码纳入诸如HasVowels()
之类的函数,该函数检查arrayCopy
是否在ITH索引处具有元音,然后在searchVowels
和searchConsonants
中使用它。
另外一件事是代码中"免费"的使用。在C 中,删除操作员只能用于指向使用new operator
分配的内存的指针,并且free()
仅应用于指向使用malloc()
分配的内存的指针,或用于null指针。
new
操作员应与delete
操作员一起使用(不与free
(一起使用。
您不应从一个功能如果返回值旨在由呼叫者使用。
在下面的此示例中,函数的呼叫者(不是功能本身(分配内存和呼叫者在不再需要时可以释放内存。
searchVowels
功能不一定知道呼叫者不再需要内存。
在下面的此示例中,searchVowels
函数不分配内存并假定dest
参数数组的内存并且输入字符串已经分配。
/* IMPORTANT: This code assumes that SIZE is already defined */
void searchVowels(char* dest, char * inputString) {
int destIndex;
int i;
/* Update this function with other functionality */
/* as intended */
destIndex = 0;
dest[destIndex] = ' ';
for(int i = 0; i < strlen(inputString); i++)
{
if ((inputString[i] == 'A') || (inputString[i] == 'E')) {
{
if (destIndex < SIZE) {
dest[destIndex] = inputString[i];
dest[destIndex+1] = ' ';
destIndex = destIndex + 1;
}
}
}
}
/* From the code that calls searchVowels */
char* result;
try {
char* result = new char[SIZE];
searchVowels(result, "TESTAEIOU");
/* Use the result variable Here */
delete result;
} catch (std::bad_alloc&) {
/* new did not allocate memory */
}
我解决了问题,它正在返回,但没有设置数据应该如何。我需要使用||元音的管道,并为循环设置另一个以检查所有条件。它与释放记忆无关。这是功能的代码。
char * searchConsonants(char * arrayCopy)
{
char * consonants; consonants = new char[SIZE];
for (int i = 0; i < 6; ++i)
{//I feel like I could make this into a function itself...hmm
for(int j = 0; j < 6; ++j)
{
if( arrayCopy[j] != 'A' && arrayCopy[j] != 'E'
&& arrayCopy[j] != 'I' && arrayCopy[j] != 'O' &&
arrayCopy[j] != 'U' && arrayCopy[j] != 'Y')
{
consonants[i] = arrayCopy[j];
++i;
}
}
}
return consonants;
}
- 从python中调用C++函数并获取返回值
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 程序不向函数返回值
- 如何在另一个函数中使用返回值作为参数?
- 编写以 C++ 为单位返回值的函数
- 了解在返回值之前和之后使用 EAX 的函数调用
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 如何将不可移动和不可复制的函数返回值获取到数组中
- G++ 编译器是否在未使用返回值的情况下将 constexpr 函数视为常规函数?
- 为什么当你将函数的返回值乘以零时它不会短路?
- 函数的返回值类型是别名 ***或 *** 布尔值
- 如何在没有返回值的情况下使用(特征)unaryExpr 和 lambda 函数?
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 为什么这个"ctypes"包装函数的返回值是"c_long(0)"而不是&quo
- 函数不显示返回值 - C++
- 结构新手,我对如何从 void 函数中返回值并将其放入另一个函数感到困惑
- 将 void 函数更改为返回值函数
- 返回值函数中结构成员赋值的额外拷贝