Chi*_*ikh 2 c debugging linked-list
这是我的代码.我很确定我已正确实现了链接列表,但我认为存在一些语法错误.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct *node next;
};
void push(struct node** headRef, int data)
{
struct node* newNode;
newNode = malloc(sizeof(struct node));
newNode->data = data;
newNode->next = *headRef;
*headRef = newNode;
}
struct node* primeFactors(int num)
{
struct node* head = NULL;
if (num == 1)
{
return head;
}
int factor = 0;
int i = 2;
while (i <= num)
{
if (num % i)
{
factor = i;
}
}
push(&head, factor);
primeFactors(num / factor);
return head;
}
int main(int argc, char const *argv[])
{
struct node* head = primeFactor(600851475143);
printf("%d/n", head->data);
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误.我不知道大多数这些意味着什么,struct node肯定应该有一个名为next的成员.
[1] $ gcc 3.c -o 3
3.c:6:9: error: expected ‘{’ before ‘*’ token
3.c: In function ‘push’:
3.c:14:9: error: ‘struct node’ has no member named ‘next’
3.c: In function ‘main’:
3.c:42:22: warning: initialisation makes pointer from integer without a cast [enabled by default]
真的很感激帮助!
您的next结构成员的星号位于错误的位置.代替
struct *node next;
Run Code Online (Sandbox Code Playgroud)
它应该是
struct node *next;
Run Code Online (Sandbox Code Playgroud)
并且你的主要功能也有一个拼写错误,这会导致initialization makes pointer from integer警告.你输入了
struct node* header = primeFactor(600851475143);
Run Code Online (Sandbox Code Playgroud)
但是你的函数的名字是primeFactors复数,所以它应该是
struct node* header = primeFactors(600851475143);
Run Code Online (Sandbox Code Playgroud)
您还使用了错误的数据类型作为primeFactors函数的参数.带符号的32位整数不能存储大到600851475143的值,因此在分配值时会溢出它.假设您正在使用的系统支持它,请使用uint64_t或unsigned long long替代int,并使用"%llu"作为printf格式.
| 归档时间: |
|
| 查看次数: |
132 次 |
| 最近记录: |