Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给定一个使用malloc创建的字符串,如何向其添加整数?_C_String_Integer_Malloc - Fatal编程技术网

给定一个使用malloc创建的字符串,如何向其添加整数?

给定一个使用malloc创建的字符串,如何向其添加整数?,c,string,integer,malloc,C,String,Integer,Malloc,为什么将整数添加到字符串时,它在打印时不会显示?代码如下: char *newStr = NULL; char *backedUpPtr = NULL; newStr = (char *) malloc ((4) * sizeof(char)); backedUpPtr = newStr; *newStr = 'a'; newStr++; *newStr = 4; printf("%s", backedupPtr); 打印时,数字4不会显示。为什么呢?我需要把它转换成字符吗?如果是,怎么

为什么将整数添加到字符串时,它在打印时不会显示?代码如下:

char *newStr = NULL;
char *backedUpPtr = NULL;

newStr = (char *) malloc ((4) * sizeof(char));
backedUpPtr = newStr;

*newStr = 'a';
newStr++;
*newStr = 4;

printf("%s", backedupPtr);

打印时,数字4不会显示。为什么呢?我需要把它转换成字符吗?如果是,怎么做?

首先,您没有用NUL字符终止字符串(因此从技术上讲它不是C字符串)。从
malloc
返回的内存包含任意字节,不一定是零

换句话说,该代码可能会给您带来麻烦,因为您没有正确终止字符串-
printf
可能会因此而崩溃成一堆尖叫

除此之外,您还存储了代码点
4
,即
ASCII
中的CTRL-D。如果需要可打印的
4
,则需要使用
'4'

而且,虽然我们列出了一长串的问题,但是变量
backedUpPtr
backedUpPtr
之间存在着巨大的差异(即
u
的资本化),这是释放分配的内存的好形式,不应该在C中使用
malloc
的返回值,它可能导致某些细微的错误。此外,与sizeof(char)相乘是不必要的,因为它总是一

总之,我将从以下代码开始,然后继续:

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

int main (void) {
    char *newStr = NULL;
    char *backedUpPtr = NULL;

    newStr = malloc (4);         // no need for cast or multiply
    if (newStr == NULL) {        // and ALWAYS check
        printf ("No memory available\n");
        return 1;
    }
    backedUpPtr = newStr;

    *newStr = 'a';               // store the a
    newStr++;
    *newStr = '4';               // store the character 4
    newStr++;
    *newStr = '\0';              // make into C string

    printf ("%s", backedUpPtr);

    free (backedUpPtr);          // also good form
    return 0;
}
或者,更简单,尽管没有教育意义:-)

#包括
内部主(空){
printf(“a4”);
返回0;
}

对不起,我打错了。newStr是char类型*来自
malloc()
的数据不能保证为零,但也不能保证不为零。这样编写的
4
是Control-D,打印时,控制字符通常没有可见的表示形式;您可能想要
'4'
。您应该以null结尾字符串。您也应该在字符串后输出一个换行符(
%s\n“
格式字符串)。请注意,您需要
backeduptr
才能
释放()
内存。
#include <stdio.h>
#include <stdlib.h>

int main (void) {
    char *newStr = malloc (4);
    char *backedUpPtr = newStr;

    if (newStr == NULL) {
        printf ("No memory available\n");
        return 1;
    }

    *newStr++ = 'a';
    *newStr++ = '4';
    *newStr   = '\0';

    printf("%s", backedUpPtr);

    free (backedUpPtr);

    return 0;
}
#include <stdio.h>

int main (void) {
    char backedUpPtr[4], *newStr = backedUpPtr;

    *newStr++ = 'a';
    *newStr++ = '4';
    *newStr = '\0';

    printf("%s", backedUpPtr);

    return 0;
}
#include <stdio.h>
int main (void) {
    printf ("a4");
    return 0;
}