小编Nat*_*ium的帖子

为什么不能在 c 中初始化 main() 之外的结构值?

我在 Visual Studio 中收到错误代码:

struct coordinates {
    int x;
    int y;
};
struct coordinates point;
point.x = 5;
point.y = 3;

int main() {
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我初始化point.xand point.yinmain()和/或如果我给出这样的点值,它就会起作用:struct coordinates point = {5, 3}。为什么不能point.xpoint.y之外初始化main()

c struct structure

2
推荐指数
2
解决办法
2590
查看次数

返回 *这引起了很大的挫败感

我已经使用 C 大约一年了,最后决定学习 C++。我正在尝试像这样实现我自己的链表类:

#include <cstdio>
#include <cstdlib>

struct linked_list_node
{
    int value;
    struct linked_list_node* next;
};

class LinkedList
{
    linked_list_node* head;
public:
    LinkedList add(int value)
    {
        printf("add called\n");
        if (head == nullptr)
        {
            head = new linked_list_node;
            head->value = value;
            head->next = nullptr;
        }
        else
        {
            linked_list_node* curr = head;
            while (curr->next != nullptr) curr = curr->next;
            curr->next = new linked_list_node;
            curr->next->value = value;
            curr->next->next = nullptr;
        }
        return *this;
    }
    int get(size_t index)
    {
        linked_list_node* curr = head; …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
67
查看次数

标签 统计

c ×1

c++ ×1

struct ×1

structure ×1