扫地浮尘 2017-11-28 14:21 采纳率: 50%
浏览 1349
已采纳

const char *,char *,char const *中vs错误问题,求指教

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中编译无此错误
求指教

  • 写回答

3条回答 默认 最新

  • 龙跃十二 博客专家认证 2017-11-30 11:34
    关注

    你定义变量太靠后了,我给你修改了

     #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;
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算