如果我有一个指向字符串变量的指针,array of chars那么键入是否有区别:
char *name = "name";
Run Code Online (Sandbox Code Playgroud)
和,
string name = "name";
Run Code Online (Sandbox Code Playgroud) 假设我想在C++中定义一个函数
int sort( int a[n], int n)
Run Code Online (Sandbox Code Playgroud)
我希望函数在没有预定义长度的数组上执行操作.我该怎么做?
我用C++写下了一个代码,使单词从字符串输入中出现在数组中.但它不会因溢出或其他原因而起作用.编译器不会显示任何错误.
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main()
{
char a[100];
gets(a);
char b[100][100];
int I=0;
for(int j=0;j<strlen(a);j++) //runs the loop for words
{
int k=0;
do
{
b[I][k]=a[j];
k++;
}
while(a[j]!=' ');
I++;
}
cout<<b[0][0];
return 0;
}
Run Code Online (Sandbox Code Playgroud)