int spitstring(const char *str, char c, char buf[10][30], int *count)//14 行
{
if (str == NULL || buf == NULL || count == NULL)
return -1;
const char *start = str;//18行
char *p = NULL;
int i = 0;
do
{
p = strchr(start, c);
if (p != NULL)
{
int len = p - start;
if (len > 0)
{
底下不写了
}}while (*start);//41行
底下不写了
int main(void)
{
const char *p = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";
char buf[10][30] = { 0 };
int n = 0;
int ret = 0;
int i = 0;
ret = spitstring(p, ',', buf, &n);
错误 1 error C2143: 语法错误 : 缺少“;”(在“const”的前面) 18行
错误 16 error C2100: 非法的间接寻址 41行
把const char *换成const char *或者char *错误都消失
这种错误只在vs中存在,linux中编译无此错误
求指教
你定义变量太靠后了,我给你修改了
#include <stdio.h>
#include <string.h>
#include <Windows.h>
int spitstring(const char *str, char c, char buf[10][30], int *count)
{
const char *start = str;
char *p = NULL;
int i = 0;
if (str == NULL || buf == NULL || count == NULL)
return -1;
start = str;
do
{
p = strchr(start, c);
if (p != NULL)
{
int len = p - start;
if (len > 0)
{
strncpy(buf[i], start, len);
buf[i][len] = 0;
i++;
}
start = p + 1;
}
else
{
strcpy(buf[i], start);
i++;
break;
}
} while (*start);
if (i == 0)
{
strcpy(buf[i], start);
return -2;
}
*count = i;
return 0;
}
int main(void)
{
const char *p = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";
char buf[10][30] = { 0 };
int n = 0;
int ret = 0;
int i = 0;
ret = spitstring(p, ',', buf, &n);
if (ret != 0)
{
printf("error:%d\n", ret);
return ret;
}
for (i = 0; i < n; i++)
{
printf("%s\n", buf[i]);
}
system("pause");
return 0;
}
vs是集成开发环境,vs编译器会对代码进行自动优化, 你能不能把所有代码复制下,我帮你看看,希望能帮你解决
int spitstring(const char *str, char c, char buf[10][30], int *count)
{
if (str == NULL || buf == NULL || count == NULL)
return -1;
const char *start = str;
char *p = NULL;
int i = 0;
do
{
p = strchr(start, c);
if (p != NULL)
{
int len = p - start;
if (len > 0)
{
strncpy(buf[i], start, len);
buf[i][len] = 0;
i++;
}
start = p + 1;
}
else
{
strcpy(buf[i], start);
i++;
break;
}
} while (*start);
if (i == 0)
{
strcpy(buf[i], start);
return -2;
}
*count = i;
return 0;
}
int main(void)
{
const char *p = "abcdef,acccd,eeee,aaaa,e3eeee,ssss,";
char buf[10][30] = { 0 };
int n = 0;
int ret = 0;
int i = 0;
ret = spitstring(p, ',', buf, &n);
if (ret != 0)
{
printf("error:%d\n", ret);
return ret;
}
for (i = 0; i < n; i++)
{
printf("%s\n", buf[i]);
}
system("pause");
return 0;
}