如何在C中使用指针而不是使用字符数组来容纳字符串? [英] How is an string being hold by a pointer and not by an character array in C?

查看:54
本文介绍了如何在C中使用指针而不是使用字符数组来容纳字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码可以完美运行;如您在这里看到的, name 是一个指针.当我给它一个值时,它会完美存储,但是当我将指针更改为数组时,代码会中断.为什么我需要一个指针来存储字符串?

This code works perfectly; as you can see here name is a pointer. When I give a value to it, it is stored perfectly, but when I change the pointer to array the code breaks. Why do I need a pointer to store a string?

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

struct student{
    char *name;
    int roll;
    int clas;
}*ptr,stu;

void main(){
    int i;
    int n;
    stu.roll=2;
    stu.name = "sum";
    printf("%d %s",stu.roll,stu.name);
}

但这不是:

#include<stdio.h>
#include<stdlib.h>
struct student{
    char name[10];
    int roll;
    int clas;
}*ptr,stu;

void main(){
    int i;
    int n;

    stu.roll=2;
    stu.name = "sum";
    printf("%d %s",stu.roll,stu.name);
}

推荐答案

不需要使用指针存储字符串.数组是指针,是处理字符串的两种有效方法,并且它们是相互绑定的某种方法.您只需要了解什么是数组以及如何将字符存储在数组中,就可以将它们称为字符串".

You don't need to use a pointer to store strings. Arrays are pointers are two valid ways to deal with strings, and they are some way mutually bound with each other. You just need to understand what an array is and how are characters stored in them so that we can call them "strings".

我们需要记住的第一件事:什么是指针(指向 T 类型)?

First thing we need to remember: what is a pointer (to a type T)?

  • 存储 T 类型的数据的地址
  • It the address where data of type T is stored

现在,什么是数组`T var [N]?

Now, what is an array `T var[N]?

  • 它是连续存储在内存中的N个相同类型 T 元素的序列.
  • var 是数组的名称,并表示其数组第一个元素的地址.它像指针一样被评估,尽管它不是指针.
  • It is a sequence of N elements of the same type T stored contiguously in memory.
  • var is the name of the array, and represents the address of its first element of the array. It is evaluated like a pointer though it's not a pointer.

快到了.什么是字符串?

  • 它是一个数组,其中包含类型为 char 的元素,这些元素以特殊的 nul-terminator 字符(''\ 0')
  • 终止
  • it is an array containing elements of type char terminated by a special nul-terminator character (`'\0')

因此,字符串 IS 是一个数组,但是就像每个数组一样,都可以将其视为指向其第一个元素的指针.如果指向 char 的每个指针都是一个字符串,如果它以空字符结尾,则可以使用数组语法进行访问:

So a string IS an array, but like every array can be evaluated as a pointer to its first element. And every pointer to char is a string if it is terminated by the null character, and be accessed with the array syntax:

char * str = "hello"; // contains 'h', 'e', 'l', 'l', 'o', '\0'
                      // cannot be modified because it points to a constant area of memory

printf ("%c %c\n", str[1], str[4]); // prints "e o"

/********/

char str2[] = "hello"; // same contents as above, but thi one can be modified


注意:作业

stu.name = "sum";

无效,因为 name struct student 结构的数组字段.如上所述,数组不是指针,主要区别之一是无法分配它(即,不能是赋值操作中的 lvalue ).它将引发编译错误.

is invalid because name is an array field of the struct student struct. As explained above an array is not a pointer, and one of the main differences is that it cannot be assigned (i.e. cannot be an lvalue in an assignment action). It will raise a compilation error.

正确的操作是使用 strcpy()函数将数据复制到阵列中.

The correct action is copying the data into the array using strcpy() function.

这篇关于如何在C中使用指针而不是使用字符数组来容纳字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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