为结构变量定义字符串值

发布于 2024-10-12 09:38:26 字数 527 浏览 7 评论 0原文

我刚刚回答了一些面试问题。遇到了与结构相关的问题,我不明白输出中发生了什么,请有人可以解释原因。

何时在结构中使用字符指针,例如

#include <stdio.h>

struct name {
    char *array;
}variable;

int main( int argc, char * argv[] ){

    variable.array="hello";
    printf( "%s\n", variable.array );
}

打印输出 hello ,但是当将结构变量更改为 时,

struct name {
    char array[10];
}variable;

编译器会抛出错误“赋值中的类型不兼容”,

variable.array="hello";

我真的很困惑我在哪里我没抓住要点。为什么会出现类似赋值问题的错误?请纠正我 谢谢

I was just going through certain interview questions. Got this structure related issue, I m not understanding what is happening wit the output, Please if some could explain the reason.

When is use a character pointer in the structure like

#include <stdio.h>

struct name {
    char *array;
}variable;

int main( int argc, char * argv[] ){

    variable.array="hello";
    printf( "%s\n", variable.array );
}

The output is hello printed, but when change the structure variable to

struct name {
    char array[10];
}variable;

The compiler throws an error "incompatible types in assignment" at

variable.array="hello";

I am really confused as to where i am missing the point. Why does it show the error like assignment problem?? Please correct me
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

往日 2024-10-19 09:38:26

您只能像在声明时那样初始化数组,
否则你需要使用

strcpy(variable.array,"hello");

你甚至不能用一个简单的字符数组来做到这一点,

char a[10];

a="hello";

编译器会告诉你:

incompatible types when assigning to type ‘char[10]’ from type ‘char *’

因为 "hello" 是一个字符串文字,它由一个不能被指针保存的字符串文字像这样分配给一个数组。

You can only initialize array like that at the time of declaration only,
else you need to use

strcpy(variable.array,"hello");

you can't even do like that with a simple char array

char a[10];

a="hello";

the complier will tell :

incompatible types when assigning to type ‘char[10]’ from type ‘char *’

because "hello" is a string literal that is being held by a pointer which can't be assigned like this to an array.

青巷忧颜 2024-10-19 09:38:26

在 C 语言中,术语 "hello" 表示“指向包含字符 'hello\0' 的字符串的指针”。
因此,当您在第一种情况下分配 array="hello" 时,您是将指向字符串的指针分配给指针变量。这是正确的。

在第二种情况下,您尝试将指针分配给数组,这是错误的!
你不能给数组赋值。

In C the term "hello" means "a pointer to a string that has the chars 'hello\0' in it".
So when you assign array="hello" in the first case you are assigning the pointer to the string into a pointer variable. Which is right.

In the second case you try to assign a pointer to an array which is wrong!
you can't assign value to an array.

云之铃。 2024-10-19 09:38:26

它会引发错误,因为 char 数组和指向 char 的指针是不同的东西。

数组不是指针。指针不是数组。有时指针可能指向数组。但即使指针指向数组,它也不是数组。

你的编译器在编译时给出了variable.array的地址。它还给出了字符串“hello”的地址。他们不是同一个地址。字符串文字和数组(或“缓冲区”)位于不同的位置;从逻辑上讲,必须将字符串复制到缓冲区中。

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

struct name {
    char array[10];
}variable;

int main( void ){

    strcpy(variable.array, "hello");
    printf( "%s\n", variable.array );
    return 0;

}

您可能会发现C 常见问题解答很有用。

It raises an error because an array of char and a pointer to char are different things.

An array is not a pointer. A pointer is not an array. Sometimes a pointer might point to an array. But even when a pointer points to an array, it's not an array.

Your compiler gives an address to variable.array at compile time. It also gives an address to the string literal "hello". They're not the same address. The string literal and the array (or "the buffer") are in different places; logically, the string has to be copied into the buffer.

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

struct name {
    char array[10];
}variable;

int main( void ){

    strcpy(variable.array, "hello");
    printf( "%s\n", variable.array );
    return 0;

}

You might find the C FAQ useful.

温柔少女心 2024-10-19 09:38:26

首先,数组是一个指向数组中第一个元素的常量指针
数组,一旦声明,我们就无法更改其地址位置..就像

int a[3]; is equivalent to int *const a;

so we cant change the array address location

int a[3]={1,2,3};

a++;//its an error

int b[3];

b=a;//its also an error

but int *c;

c=a;//Not an error because c is a pointer to integer not constant pointer to integer

similar to 
char str[10]="hello";

if i change like this

str="world";//its also an error

Pointers and arrays are always not equal, in some cases only like dereferencing 

   char a[10]="hello";
for(i=0;a[i]!='\0';i++)
   printf("%c",a[i]);

for(i=0;a[i]!='\0';i++)
   printf("%c",*(a+i));

The first thing is array is a constant pointer to the first element in the
array,we can not change its address location once we declared..like

int a[3]; is equivalent to int *const a;

so we cant change the array address location

int a[3]={1,2,3};

a++;//its an error

int b[3];

b=a;//its also an error

but int *c;

c=a;//Not an error because c is a pointer to integer not constant pointer to integer

similar to 
char str[10]="hello";

if i change like this

str="world";//its also an error

Pointers and arrays are always not equal, in some cases only like dereferencing 

   char a[10]="hello";
for(i=0;a[i]!='\0';i++)
   printf("%c",a[i]);

for(i=0;a[i]!='\0';i++)
   printf("%c",*(a+i));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文