表示一个指针 [英] Represent a pointer

查看:34
本文介绍了表示一个指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果ListofChar是一个指向Char的指针(不要与char混淆),它代表struct charact
那么void report(ListofChar chars)中的ListofChar chars是什么意思?
chars 是否只代表指针ListofChar?

If ListofChar is a pointer to Char (do not confuse with char) which represents struct charact,
then what does it mean the ListofChar chars in void report(ListofChar chars)?
Does chars just represents the pointer ListofChar?

#include <stdio.h>
#include <stlib.h>

struct charact {
     char ch;
     int occurs;
     struct charact *next;
     };

   typedef struct charact Char;
   typedef Char * ListofChar;
   typedef Char * CharNode_ptr;

  void letters(char name[50], ListofChar * chars_ptr);
  void report(ListofChar chars);
  Char * createnode(char ch);

 int main() {
      char name[50];
      ListofChar chars = NULL;
      scanf("%s", name);
      letters(name, &chars);
      report(chars);
      return 0;
      }
   Char * createnode(char ch) {
   CharNode_ptr newnode_ptr ;
   newnode_ptr = malloc(sizeof (Char));
   newnode_ptr -> ch = ch;
   newnode_ptr -> occurs = 0;
   newnode_ptr -> next = NULL;
   return newnode_ptr;
   }

推荐答案

这个回答是基于我对你的问题的理解,如下,我认为你的问题在于使用指针和指针声明数组.

This answer is based on my understanding of your question, which is as follows, I think your problem lies with the declaration of an array using pointers and a pointer.

如您所知,每个变量都是一个内存位置,并且每个内存位置都定义了其地址.任何给定类型的指针都包含指向内存位置的地址.对于您的情况,请考虑以下示例.

As you know, every variable is a memory location and every memory location has its address defined. A pointer of any given type contains the address to the memory location. For your case, consider the following example.

struct charact * a;

struct charact * a;

我们定义了一个类型为 'struct charact' 的指针 'a' 到目前为止,指针 'a' 持有一个垃圾值,如果你试图给它分配一些东西会给你一个错误,因为当你分配给你时,系统会转到'a' 占用垃圾值并将其视为内存地址,它必须在其中放置给定值,这是无法完成的,因为垃圾地址可能没有分配给您.为了在 'a' 处分配一个值,您首先必须使用 malloc() 或任何其他内存分配函数来初始化 'a'.这样做时 malloc() 会向您返回一个内存地址,然后您就可以开始并为 'a' 赋值.

we define a pointer 'a' of type 'struct charact' as of now the pointer 'a' holds a garbage value and if you try to assign something to it will give you an error because when you assign you system goes to 'a' takes up the garbage value and think of it as a memory address where it has to put the given value which cannot be done because the garbage address is probably not assigned to you. In order to assign a value at 'a' you first have to initialise 'a' by using malloc() or any other memory allocation functions. On doing so malloc() would return a memory address to you and then you can start and assign values to 'a'.

现在回到您的问题,定义 'a' 可以充当数组或指向结构的指针.这取决于您使用 malloc() 为变量分配了多少内存.您可能会按如下方式分配值 -

Now coming to your question, defining 'a' can act as an array or as a pointer to the structure. This depends on how much memory you are assigning to the variable using malloc(). You would probably assign the value as follows -

a = (struct charact *)malloc(sizeof(struct charact)*n)

a = (struct charact *)malloc(sizeof(struct charact)*n)

如果n>1,那么您将获得一个内存地址,该地址能够容纳更多结构字符,因此将作为数组工作,如果n=1,您只能容纳一个结构字符,因此它是单个实体.

if n>1, then you will get a memory address which is capable of holding more then on struct charact and hence will work as an array, and, if n=1 you can hold only one struct charact and hence it is a single entity.

这篇关于表示一个指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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