相关疑难解决方法(0)

cpp空数组声明

您好我有以下测试代码,我对cpp感到困惑.

  1. 如果你在library.h中声明一个带有空元素子句的数组..编译器会选择什么?它也没有抱怨,我使用Cygwin.

  2. 在library.cpp中我为两个元素赋值,是编译器假设一个元素有一个元素,我写第二个元素在数组范围之外?

library.h

#ifndef LIBRARY_H
#define LIBRARY_H

class library {

public:
    void print();
    char a[];
};

#endif
Run Code Online (Sandbox Code Playgroud)

library.cpp

#include <stdio.h>
#include "library.h"

void library::print() {
    a[0] = 'a';
    printf("1. element: %d\n", a[0]);
    a[1] = 'b';
    printf("2. element: %d\n", a[1]);
}
Run Code Online (Sandbox Code Playgroud)

client.cpp

#include <stdio.h>
#include "library.h"

void execute();
library l;

int main() {
    l = library();
    l.print();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Makefile文件

OPTIONS=-Wall

all: main

run: main
        ./main.exe

main: client.o library.o
        g++ $(OPTIONS) -o main $^

library.o: library.cpp library.h
        g++ $(OPTIONS) …
Run Code Online (Sandbox Code Playgroud)

c++

8
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1