Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.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/3/arrays/12.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
如何根据用户输入的长度动态分配char变量的数组大小?_C_Arrays_Pointers_Heap - Fatal编程技术网

如何根据用户输入的长度动态分配char变量的数组大小?

如何根据用户输入的长度动态分配char变量的数组大小?,c,arrays,pointers,heap,C,Arrays,Pointers,Heap,我需要让用户输入他的名字并将其存储在一个数组char中,这个数组的大小根据他输入的用户名动态定位 这是我的代码: #include <stdio.h> void main() { static int size = 5 ; printf("please Enter Your First Name..\n"); char* p = (char*)malloc(size*sizeof(char)); for(int i = 0 ; i <= si

我需要让用户输入他的名字并将其存储在一个数组char中,这个数组的大小根据他输入的用户名动态定位

这是我的代码:

#include <stdio.h>
void main()
{
    static int size = 5 ;
    printf("please Enter Your First Name..\n");

    char* p = (char*)malloc(size*sizeof(char));

    for(int i = 0 ; i <= sizeof(p) ; i++)
    {
        if(sizeof(p)+1 == size )
        {
            p = (char*)realloc(p,2*size*sizeof(char));
            size = sizeof(p);
        }
        else
        {
            scanf("%c",&p[i]);
        }

    }
    for(int i = 0 ; i <=size ; i++)
    {
        printf("%s",*(p+i));
    }

free(p);

}
我给数组的第一个大小是5个字符,如果用户名长度大于这个大小,那么它在堆中的realloc是这个大小的两倍

我做了一个条件,如果sizeofp+1==size,因为sizeofp=4,所以我需要5=5,所以我把sizeofp+1放进去


但是我的代码不起作用。为什么?

一个常见的错误是使用sizeofpointer获取它所指向的内存的大小。下面是一些示例代码供您尝试

#include <stdio.h>
#include <string.h>
#include <stdlib.h>             // required header files

#define CHUNK 1  // 8           // amount to allocate each time

int main(void)                  // correct definition
{
    size_t size = CHUNK;        // prefer a larger size than 1
    size_t index = 0;
    int ch;                     // use the right type for getchar()
    char *buffer = malloc(size);
    if(buffer == NULL) {
        // handle erorr
        exit(1);
    }

    printf("Please enter your first name\n");
    while((ch = getchar()) != EOF && ch != '\n') {
        if(index + 2 > size) {       // leave room for NUL
            size += CHUNK;
            char *temp = realloc(buffer, size);
            if(temp == NULL) {
                // handle erorr
                exit(1);
            }
            buffer = temp;
        }
        buffer[index] = ch;
        index++;
    }

    buffer[index] = 0;               // terminate the string
    printf("Your name is '%s'\n", buffer);
    printf("string length is %zu\n", strlen(buffer));
    printf("buffer size   is %zu\n", size);

    free(buffer);
    return 0;
}
我更喜欢使用较大的缓冲区大小,因为这对系统的压力较小,而且因为分配的内存可能使用最小大小。当我设定

#define CHUNK 8
会议正在进行中

Please enter your first name
Wilhelmina
Your name is 'Wilhelmina'
string length is 10
buffer size   is 16

我会分配一个足够大的缓冲区,然后通过重新分配来缩小大小。为每个字符重新定位是非常低效的。如果您必须在IMO中使用这种方法,那么最好以8个字节的块为单位工作,换句话说,当空间用完时,您将分配8个字节。内存分配的粒度不太可能比这更好,因此您不会浪费内存。@WeatherVane,我认为应该创建缓冲区,但我认为应该是相同的,因为我给了数组的初始大小SizeOff是什么?这是缓冲区指针的大小,而不是缓冲区的大小。除非使用自己的变量跟踪内存分配,否则无法获得内存分配的大小。您需要两个变量,一个用于缓冲区大小,另一个用于已使用的量。@指针的风向标大小,嗯,SizeOffp+1将始终给出相同的值,无论您进行多少重新分配。谢谢您,大师,您让我度过了美好的一天,但是我有一个问题ifindex+2>size这个条件的目的是什么?缓冲区必须足够大,以容纳下一个字符和循环后写入的NUL终止符。数组的初始大小为1,如果第一个字符是换行符,则得到空字符串。hello bro,buffer[index]=ch是什么意思;为什么我需要这条线?并且该缓冲区[索引]=0;缓冲区[索引]=ch;正在生成键入的字符数组。缓冲区[索引]=0;正在编写使其成为字符串所需的nul终止符。
Please enter your first name
Wilhelmina
Your name is 'Wilhelmina'
string length is 10
buffer size   is 16