C 指针、链表和节点

标签 c pointers struct linked-list nodes

我已经研究这个代码好几天了,这就是我想到的地方(我之前在这里问过)。代码要点是打印给定字符串中每个字母出现的次数,仅更改 letters()report()

我在 letters() 中添加了 printf 以便查看发生了什么。我不太确定在函数 letters() 中是否需要第一个循环,但它正在打印整个输入的字符串。另外,如果我不将第一个循环放在 letters() 中,那么在 report() 中,当前是 NULL(为什么???) 。 letters() 中的第二个循环仅打印输入字符串的最后一个字母(为什么?)。 report() 再次只打印字符串的最后一个字母,而 current->occurrences 打印一个随机的 6 位数字(可能是 mem 地址?),还报告了永远开启,这意味着当前永远不会变成NULL??? 。我已经为此工作好几天了,我无法理解为什么 letters()report() 上的第二个循环不起作用,它们对我来说似乎是正确的。任何帮助表示赞赏...

#include <stdio.h>
#include <stdlib.h>
#include <string.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;
}


void letters(char name[50],ListofChar* lst_ptr){
    ListofChar newnode ;
    newnode =(Char*)malloc(sizeof(Char));
    if(!newnode){printf("adinamia mnimis");}
    int i,j;

    for( i = 0 ; i < strlen(name)  ; i++){
        newnode->ch  = name[i] ;
        printf("Look: %c\n",newnode->ch);
        newnode->next = *lst_ptr ;
        *lst_ptr = newnode ;
        }

    for( i = 0 ; i < 50 ; i++){
        for( j = 0 ; j < strlen(name) ; j++){
            if  (newnode->ch == name[i]){
                newnode->occurs++;
                printf("uhm : %c\n",newnode->ch) ;
                newnode = newnode->next;
                break;
                }
            printf("hey : %c\n",newnode->ch);
        }
    }

}




void report(ListofChar chars) {
    ListofChar current  ;
    current = chars ;
    printf("the occurences of each letter are:\n");
    while(current != NULL){
        printf("%c :  %d\n", current->ch,current->occurs );
        current = current->next ;
    }
    if(current==NULL)printf("fail");
    return;
}

最佳答案

Also,if i dont put the first loop in letters() then in report() current is NULL(why???).

  • 它是 NULL,因为你没有用它做任何事情。您将其初始化为 NULL,然后将其传递给函数,没有对它执行任何操作,当它到达 report() 时,它仍然是 NULL。

The second loop in letters() prints only the last letter of the inputed string(again why?).

  • 它只打印最后一个字母,因为在 letters() 的第一个循环中,您唯一要做的就是将 newnode->ch 分配给当前字母,并且当循环退出时,它将具有最后一个字母的值。

And report() again only prints the last letter of the string and the current->occurences prints a random 6-digit number(probably mem adress?

  • report() 打印一个随机数,因为你不知道是什么 在 letters() 中递增 newnode->occurrs 值之前。

also reports goes on for ever, which means current never becomes NULL???

  • 它会永远消失,因为 current 总是指向自身(letters() 中第一个循环中的逻辑已损坏。

这段代码错误太多。我建议您坐下来阅读更多有关 C 指针的基础知识。

关于C 指针、链表和节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50239521/

相关文章:

c++ - C++中的Socket connect()函数解释/Reference operator and Pointers

C- 使用位域时结构的大小以及它在内存中的存储方式

c - eclipse CDT : View structure hierarchy

c - 替换字符数组中的字符

比较使用 fgets 和 fscanf 获取的字符串

c - 在 C 宏中执行算术运算

c - (结构体名称*)b->c 是什么意思?

调用 C 中的特定线程

c - 在 C 中使用 strtok 将日期字符串转换为整数

C++双指针成员访问