为什么只给字符分配一个字符指针,而给它分配一个字符串呢? [英] Why character pointer is getting assigned just a character, while a string is supposed to get assigned to it?

查看:97
本文介绍了为什么只给字符分配一个字符指针,而给它分配一个字符串呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个字符串包含三个由空格分隔的部分.前两部分是字符串,第三部分是整数.

A string contains three parts separated by space. First two parts are strings and the third part is an integer.

以下程序的输出令我惊讶.

The output of the below program is surprising me.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void split(char *ch,char **part1,char **part2,int *num){
    int length=strlen(ch), *lc1,*lc2;
    int i=0,j=0;
    printf("value of length is %d\n",length);

    printf("this function recieved %s for splitting into pieces\n",ch);

    lc1=(char *)malloc(length*sizeof(char));
    lc2=(char *)malloc(length*sizeof(char));

    while(ch[i]!=' '){
        lc1[i]=ch[i];
        printf("\nin loop with lc1[i] = %c and ch[i] =  %c",lc1[i],ch[i]);
        i++;
    }
    lc1[i]='\0';
    i++;
    while(ch[i]!=' '){
        lc2[j]=ch[i];
        printf("\nin loop with lc2[j] = %c and ch[i] =  %c",lc2[j],ch[i]);
        j++;
        i++;
    }
    lc2[j]='\0';
    i++;
    *num=atoi(&ch[i]);

    *part1=lc1;
    *part2=lc2;

    printf("\nsplit results are:\n");
    printf("part1=%s and part2=%s and num=%d and lc1=%s lc2=%s    //this is surprising me",*part1,*part2,*num,lc1,lc2);

}

int main()
{
    int N,i,j,n,*numArray,count=0;
    char **arr,*part1,*part2,*token;
    scanf("%d",&N);
    arr=malloc(N*sizeof **arr);
    numArray=malloc(N*sizeof *numArray);
    for(i=0;i<N;i++){
    arr[i]=(char *)malloc(50*sizeof(char));
    }

    for(i=0;i<N;i++){
        printf("plz enter %d th :",i);
        scanf(" ");
        gets(&arr[i][0]);
    }

    for(i=0;i<N;i++){
        printf("%s",arr[i]);
    }

    for(i=0;i<N;i++){
    /*token=strtok(arr[i]," ");
    part1=token;
    token=strtok(NULL," ");
    part2=token;
    token=strtok(NULL," ");
    n=atoi(token);*/
        split(arr[i],&part1,&part2,&n);
    //some logic to use part1 and part2 of the sentence
}
return 0;

}

我提供的输入如下:

1
abcd efgh 2

输入的第一行包含我要继续进行的句子数. 我得到的输出如下:

The first line of the input contains number of sentences I want to proceed. Output I got is as below:

plz enter 0 th :abcd efgh 2value of length is 11
this function recieved abcd efgh 2 for splitting into pieces

in loop with lc1[i] = a and ch[i] =  a
in loop with lc1[i] = b and ch[i] =  b
in loop with lc1[i] = c and ch[i] =  c
in loop with lc1[i] = d and ch[i] =  d
in loop with lc2[j] = e and ch[i] =  e
in loop with lc2[j] = f and ch[i] =  f
in loop with lc2[j] = g and ch[i] =  g
in loop with lc2[j] = h and ch[i] =  h
split results are:
part1=a and part2=e and num=2 and lc1=a lc2=e    //this is surprising me
Success time: 0 memory: 2296 signal:0
plz enter 0 th :abcd efgh 2value of length is 11
this function recieved abcd efgh 2 for splitting into pieces

in loop with lc1[i] = a and ch[i] =  a
in loop with lc1[i] = b and ch[i] =  b
in loop with lc1[i] = c and ch[i] =  c
in loop with lc1[i] = d and ch[i] =  d
in loop with lc2[j] = e and ch[i] =  e
in loop with lc2[j] = f and ch[i] =  f
in loop with lc2[j] = g and ch[i] =  g
in loop with lc2[j] = h and ch[i] =  h
split results are:
part1=a and part2=e and num=2 and lc1=a lc2=e    //this is surprising me

尽管split()中的循环函数已针对字符串中的所有字符成功执行,但我在part1(也在lc1中)和part2(也在lc2中)中仅得到了一个字符.此输出的原因是什么?

Although the loop function inside the split() is getting executed successfully for all the characters in the string, yet I am getting just a character in part1 (also in lc1) and part2 (also in lc2). What is the reason for this output?

推荐答案

此行是错误的:

arr=malloc(N*sizeof **arr);

应为:

arr=malloc(N*sizeof *arr);

**arr是一个char,因此它仅为N个字节分配空间,但是您需要为N个指针(4个字节)分配空间.因此,您没有分配足够的空间,而是在数组范围之外进行写入,从而导致行为未定义.

**arr is a char, so it's only allocating space for N bytes, but you need space for N pointers, which are 4 bytes. So you're not allocating enough space, and then you're writing outside the array bounds, resulting in undefined behavior.

演示

这篇关于为什么只给字符分配一个字符指针,而给它分配一个字符串呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆