C++ 在C+中反转字符数组+;使用指针
因此,程序应该使用一个循环来计算包含“Hello World!”的字符数组的总大小,将该计数作为int返回,然后调用一个函数,该函数使用指针从数组的开头和结尾开始交换字符,直到单词之间的空点“”。我已经大致了解了代码,但它无法正确编译,无法显示或无法完成交换,我不断遇到以下错误: “LNK1168无法打开Lab06.exe进行写入”C++ 在C+中反转字符数组+;使用指针,c++,arrays,pointers,c-strings,C++,Arrays,Pointers,C Strings,因此,程序应该使用一个循环来计算包含“Hello World!”的字符数组的总大小,将该计数作为int返回,然后调用一个函数,该函数使用指针从数组的开头和结尾开始交换字符,直到单词之间的空点“”。我已经大致了解了代码,但它无法正确编译,无法显示或无法完成交换,我不断遇到以下错误: “LNK1168无法打开Lab06.exe进行写入” 我怎样才能解决这个问题 #include "targetver.h" #include <iomanip> #include <array>
我怎样才能解决这个问题
#include "targetver.h"
#include <iomanip>
#include <array>
#include <stdio.h>
#include <tchar.h>
#include <string>
using namespace std;
int main()
{
//int to hold size of array
int count = 0;
// declare a c-string to reverse
char myString[] = "Hello world!";
//print array as is
cout << myString << endl;
//find size of the array
// call the reverser function
reverser(myString, count);
// output the result
cout << myString << endl;
system("PAUSE");
return 0;
}
int findSize(char myString[], int count)
{
count = (unsigned)strlen(myString);
cout << "The size of the Array is: " << count << endl;
return count;
}
void reverser(char myString[], int count)
{
findSize(myString, count);
char *strPtr;
char *endPtr;
endPtr =& myString[count];
strPtr = myString;
for (int x = 0; x <= count; x++)
{
strPtr++;
*strPtr = myString[x];
endPtr--;
*endPtr = myString[count--];
*strPtr = *endPtr;
cout << strPtr << endl;
cout << endPtr << endl;
}
}
#包括“targetver.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
//int来保存数组的大小
整数计数=0;
//声明要反转的c字符串
char myString[]=“你好,世界!”;
//按原样打印数组
库特
因为findSize返回count,所以需要count等于它返回的值
void reverser(char myString[])
{
int count = findSize(myString);
char *endPtr;
endPtr =& myString[count-1];
char *temp = new char[count];
for (int x = 0; x < count; x++)
{
temp[x] = *endPtr;//this assgins the last letter of myString to the first element of temp
endPtr--;//decrement so endPtr points to myString[count - 1];
}
//now transfer
for(int x = 0; x < count; x++){
myString[x] = temp[x];
}
delete [] temp;
}
void反向器(char myString[]
{
int count=findSize(myString);
char*endPtr;
endPtr=&myString[count-1];
字符*临时=新字符[计数];
对于(int x=0;x
因为findSize返回count,所以需要count等于它返回的值
void reverser(char myString[])
{
int count = findSize(myString);
char *endPtr;
endPtr =& myString[count-1];
char *temp = new char[count];
for (int x = 0; x < count; x++)
{
temp[x] = *endPtr;//this assgins the last letter of myString to the first element of temp
endPtr--;//decrement so endPtr points to myString[count - 1];
}
//now transfer
for(int x = 0; x < count; x++){
myString[x] = temp[x];
}
delete [] temp;
}
void反向器(char myString[]
{
int count=findSize(myString);
char*endPtr;
endPtr=&myString[count-1];
字符*临时=新字符[计数];
对于(int x=0;x
好,让我们从顶部开始
findsize返回大小,但您忽略了它。您尝试将count作为参数传入,并希望更新findsize中的count。C不是这样工作的。所以首先要做
int findSize(char myString[])
{
int count = strlen(myString);
cout << "The size of the Array is: " << count << endl;
return count;
}
这将返回长度的计数。如果字符串为“abc”,则计数将为3
该字符串有3个字符myString[0]、myString[1]和yststring[2]。请注意,没有[3]字符
原地倒车很棘手
void reverser(char myString[])
{
int count = findSize(myString);
char *strPtr = myString
char *endPtr = &myString[count - 1];
strPtr = myString;
for (int x = 0; x <= count / 2; x++)
{
char swap = *strPtr;
strPtr[x] = *endPtr;
*endPtr = swap;
endPtr--;
}
}
void反向器(char myString[]
{
int count=findSize(myString);
char*strprtr=myString
char*endPtr=&myString[count-1];
strprpr=myString;
对于(int x=0;xOK),让我们从顶部开始
findsize返回大小,但您忽略了它。您尝试将count作为参数传入,并希望更新findsize中的count。C不是这样工作的。所以首先要做
int findSize(char myString[])
{
int count = strlen(myString);
cout << "The size of the Array is: " << count << endl;
return count;
}
这将返回长度的计数。如果字符串为“abc”,则计数将为3
该字符串有3个字符myString[0]、myString[1]和yststring[2]。请注意,没有[3]字符
原地倒车很棘手
void reverser(char myString[])
{
int count = findSize(myString);
char *strPtr = myString
char *endPtr = &myString[count - 1];
strPtr = myString;
for (int x = 0; x <= count / 2; x++)
{
char swap = *strPtr;
strPtr[x] = *endPtr;
*endPtr = swap;
endPtr--;
}
}
void反向器(char myString[]
{
int count=findSize(myString);
char*strprtr=myString
char*endPtr=&myString[count-1];
strprpr=myString;
对于(int x=0;x),首先显示您收到的错误消息“LNK1168无法打开Lab06.exe进行写入,这意味着您的上一个版本可能仍在运行。或者您的防病毒软件正在阻止您。@drescherjm-更有可能是最后一次运行。@drescherjm如果他按预期传入计数,则可能是UB,但事实上他传入了0重新启动您的电脑。首先,显示您收到的错误消息"LNK1168无法打开Lab06.exe进行写入,这意味着您的上一个版本可能仍在运行。或者您的防病毒软件正在阻止您。@drescherjm-更可能是最后一次运行。@drescherjm如果他按预期传入计数,则它将是UB,但事实上他传入了0重新启动您的电脑。代码将不会运行。char temp弄乱了第43行:错误表达式必须具有常量value第43行:错误C2131表达式未计算为常量第47行:错误C3863数组类型“char[count]”不可赋值创建了全新的程序,通过代码进行了移植,出现了相同的错误。错误发生在char temp[count+1]的行上;再次出现在temp[x]=*endPtr;@jonthie而不是char temp[count+1]尝试放置char temp[13VLA是gcc支持的非标准扩展,msvcno不支持。将另一个int定义为count+1,然后将const int定义为第一个int不起作用。将const放在函数外部也不起作用,因为它是一个“非数值函数”代码不会运行。char temp弄乱了第43行:错误表达式必须有一个常量值第43行:错误C2131表达式未计算为常量行47:错误C3863数组类型“char[count]”不可赋值创建了全新的程序,通过代码进行了移植,出现了相同的错误。错误出现在char temp[count+1]的行中;再次出现在temp[x]=*endPtr;@jonthie而不是char temp[count+1]尝试放置char temp[13VLA是gcc支持的非标准扩展,而不是msvcno支持的扩展。将另一个int定义为count+1,然后将const int定义为第一个int不起作用。将const置于函数外部也不起作用,因为它是一个“非数值函数”。请尝试使用代码“!H!!!!!orld!”而不是反向数组…不太确定这是如何发生的。您需要ecrement endPtr each loop。--endPtr;after*endPtr=swap;
尝试代码“!H!!!!!orld!”而不是反向数组…不太确定这是如何发生的您需要递减endPtr each loop。--endPtr;after*endPtr=swap;