简体   繁体   English

用char * p定义字符串是正确的; 以后再分配价值?

[英]Is correct to define a string with char *p; and assign value later?

I want to create a string without knowing the exact dimension of it, is this correct or it will have unpredictable behavior? 我想创建一个字符串,而不知道它的确切尺寸,这是正确的,还是会有无法预测的行为?

char *p;
p="unknow string size";

If this is wrong, how i can create something similar, and modify it with string.h fucntion? 如果这是错误的,我如何创建类似的东西,并使用string.h功能对其进行修改?

[edit]I read again the answer and it was not completly clear, my first doubt is: are those two codes equals? [edit]我再次阅读了答案,但答案还不是很清楚,我的第一个疑问是:这两个代码是否相等?

char *p="unknow string size"

and

char *p;
p="unknow string size";

The only solution in C is to use realloc function C语言中唯一的解决方案是使用realloc函数

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

int main(void)
{
    char *s = malloc(1);
    printf("Enter a string: \t"); // It can be of any length
    int c;
    int i = 0;
    while((c = getchar()) != '\n' && c != EOF)
    {
        s[i++] = c;
        s = realloc(s, i+1);
    }
    s[i] = '\0';
    printf("Entered string: \t%s", s);
    free(s);
    return 0;
}  

are those two codes equals? 这两个代码相等吗?

 char *p="unknown string size" 

and

 char *p; p="unknown string size"; 

No. First snippet is declaring p as a pointer to char and initializing it to point to a string literal "unknown string size" . 否。第一个代码段是将p声明为char的指针,并将其初始化为指向字符串文字"unknown string size" In second snippet, p is defined as a pointer to char and then an assignment is done to it. 在第二个片段中, p被定义为指向char的指针,然后对其进行赋值。

It depends, you don't assign the value, y point to the address where "unknow string size" is stored, so you should know that for example you can't modify the string. 这取决于您是否不分配值,y指向存储"unknow string size"的地址,因此您应该知道例如您无法修改字符串。

If you don't want to modify the string then it's ok, but you should also protect the pointer for trying to write to that memory location cause that would be undefined behavior, you can do this 如果您不想修改该字符串,那么可以,但是您还应该保护指针以尝试写入该内存位置,因为这可能是未定义的行为,您可以执行此操作

const char *p;
p = "unknow string size";

this will not prevent you from modifying the string, but then you would need to explicitly cast the const qualifier away to do so. 这不会阻止您修改字符串,但是您需要显式地删除const限定符才能这样做。

If you intend to modify the string later, then this is not the way to do it, in that case you should do this 如果您打算稍后修改字符串,那么这不是解决问题的方法,在这种情况下,您应该这样做

char *p;
size_t length;

length = strlen("unknow string size");
p = malloc(1 + length);
if (p == NULL)
    memoryAllocationProblemDoNotContinue();
strcpy(p, "unknow string size");
 .
 .
 .
 /* use p here */
 .
 .
 .
 free(p);
I would write the function like this:

#include <stdio.h> // printf(), getline()
#include <stdlib.h>
#include <string.h> // strlen()

int main(void)
{
    char *s = NULL;

    printf("Enter a string: \t"); // It can be of any length
    fflush(stdout);

    getline(&s, 0, stdin); // note: first param is **char

    if( NULL != s )
    { // then getline successful

        // trim trailing newline
        if( '\n' == s[strlen(s)-1] ) s[strlen(s)-1] = '\0'; 

        printf("Entered string: \t%s", s);
        free(s);
    }  // end if
    return 0;
} // end function: main

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM