程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了数组分配的 GCC 编译错误大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决数组分配的 GCC 编译错误?

开发过程中遇到数组分配的 GCC 编译错误的问题如何解决?下面主要结合日常开发的经验,给出你关于数组分配的 GCC 编译错误的解决方法建议,希望对你解决数组分配的 GCC 编译错误有所启发或帮助;

我正在尝试将字符串转换为 C 语言中的等效矩阵形式。该矩阵将根据需要有 3 行和尽可能多的列。下面的代码没有编译通过,我还没弄清楚出了什么问题。

GCC 抛出的错误是:

app.c:10:25: error: subscripted value is not an array,pointer,or vector
      printf("%d\n",arr[i][k]);
                     ~~~^~
1 error generated.
@H_607_9@

主文件(app.c):

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

int main() {

  char source[] = "This is the source. "; // placeholder text
  int arr = convert(sourcE);
  for (int i = 0; i < 21; i++) {
    for (int k = 0; k < 3; k++) {
      printf("%d\n",arr[i][k]); // error occurs at this line.
    }
  }

  return 0;
}
@H_607_9@

converter.c 文件:

// Converts an input String to its respective ASCII matrix.

#include <String.h>
#include <stdio.h>
#include "converter.h"

// Converts the entire String into an multi-dimensional array.
int convert(char text[]){

    // copy the input text into a local store.
    char store[strlen(text)];
    strcpy(store,text);

    // make sure the length of the input String is a multiple of 3 or make it so.
    int excess = strlen(storE)%3;
    char excess_spaces[3] = "   ";
    if (excess != 0) {
      strncat(store,excess_spaces,3-excess);
    }

    // covert the sourcE into an array
    int arr[3][strlen(storE)/3];
    int steps = strlen(storE)/3;
    for (int i = 0; i < steps; i++) {
      int t[3];
      for (int k = 0; k < 3; k++) {
        t[k] = (int) store[3*i+k];
        arr[k][i] = t[k];
      }
    }

    return arr;

}
@H_607_9@

converter.h 文件:

int convert(char text[]);
@H_607_9@ 

解决方法

此代码中存在多个问题。

  1. 为字符串分配存储空间,必须包含一个字节作为空终止符。替换:
char store[strlen(text)];
@H_607_9@

与:

char store[strlen(text) + 1];
@H_607_9@

另外,store 必须足够大以包含最多 3 个空格的 excess

char store[strlen(text) + 3 + 1];
@H_607_9@
@H_874_57@
  • 在 C 中,您不能使用数组作为值。在每个上下文中,它都被转换为指向它的第一个元素的指针。因此无法直接返回数组。可以通过用结构包装数组来解决这个问题,但它是另一天的主题。
  • 结果 return arr 将等价于 return &arr[0],它是 int (*)[XXX] 一个指向 int 大小为 XXX 的数组的指针。

    1. 永远永远不会返回指向具有自动存储的对象的指针。这是未定义的行为。我知道目的是返回一个数组而不是指向它的指针。使用类似 @H_756_7@malloc 的函数创建一个动态存储的对象,以安全地返回一个指针。

    2. 无法按值返回可变长度数组 (VLA),因为无法在文件范围内定义可变修改 (VM) 类型。

    3. 看起来索引被交换了:

    printf("%d\n",arr[i][k]);
    @H_607_9@
    

    我想应该是 arr[k][i]

    现在...让我们解决它。

    返回 VLA 很棘手。一种解决方案是将指向 VLA 的指针作为参数传递。见https://stackoverflow.com/a/14088851/4989451。 此解决方案的问题在于调用者必须能够计算维度。

    convert() 的结果包装到 struct 的另一种方式。请注意,函数和结构可以共享名称。结果的大小为 n 和 @H_756_7@m 成员,数据指针为 arr。调用者需要将其转换为正确的 VM 类型。

    要在非平凡指针类型之间进行繁琐的转换,可以通过 void* 进行转换。

    当数组的所有工作完成后,用 free() 释放它的内存。

    // Converts an input String to its respective ASCII matrix.
    
    #include <String.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    // Converts the entire String into an multi-dimensional array.
    struct convert {
        int n,m;
        int *arr;
    } convert(char text[]){
    
        // copy the input text into a local store.
        size_t textlen = strlen(text);
        char store[textlen + 3 + 1];
        strcpy(store,text);
    
        // make sure the length of the input String is a multiple of 3 or make it so.
        int excess = textlen % 3;
        char excess_spaces[3] = "   ";
        if (excess != 0) {
          strncat(store,excess_spaces,3-excess);
        }
        size_t storelen = strlen(storE);
    
        // allocate VLA with dynamic storage
        int (*arr)[storelen / 3] = malloc(3 * sizeof *arr);
    
        // covert the sourcE into an array
        int steps = storelen / 3;
        for (int i = 0; i < steps; i++) {
          int t[3];
          for (int k = 0; k < 3; k++) {
            t[k] = (int) store[3*i+k];
            arr[k][i] = t[k];
          }
        }
    
        return (struct convert){ .n = 3,.m = steps,.arr = (int*)arr };
    } 
    
    int main() {
    
      char source[] = "This is the source. "; // placeholder text
    
      struct convert res = convert(sourcE);
      int n = res.n,m = res.m;
      int (*arr)[m] = (void*)res.arr;
    
      for (int i = 0; i < n; i++,puts("")) {
        for (int k = 0; k < m; k++) {
          printf("%d ",arr[i][k]); // error occurs at this line.
        }
      }
      free(arr);
    
      return 0;
    }
    @H_607_9@

    大佬总结

    以上是大佬教程为你收集整理的数组分配的 GCC 编译错误全部内容,希望文章能够帮你解决数组分配的 GCC 编译错误所遇到的程序开发问题。

    如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
    标签:编译错误