无法为文件变量赋值

标签 c arrays gtk

<分区>

我是 C 的新手,我试图用函数的结果填充文件范围的数组变量,这是一个简单的代码示例来说明我的意思,谁能指出我为什么这不起作用的方向?

#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>

static gchar *external_names;

void directories(int arraylength, gchar internal_names[][100]){
    int n;
    for (n = 0; n < arraylength; n++)
    {
        strcpy(external_names[n], internal_names[n]);
    }
    for (n = 0; n < arraylength; n++)
    {
        printf("%s internal with %s external\n",internal_names[n], external_names[n]);
    }
}

void main()
{
    gchar anotherarray[10][100];
    directories(10, anotherarray);
}

[编辑] 最新代码

#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>

static gchar *external_names[100];

void directories(int arraylength, gchar internal_names[][100]){
    int n = 0;
    //gchar external_names[arraylength][100];
    for (n = 0; n < arraylength; n++)
    {
        printf("%s %i\n","before", n);
        strcpy(external_names[n], internal_names[n]);
        printf("%s %i\n","after", n);
    }
}

void main()
{
    int n;
    gchar anotherarray[10][100];
    for (n = 0; n < 10; n++)
    {
        strcpy(anotherarray[n],"test");
    }
    directories(10, anotherarray);
    for (n = 0; n < 10; n++)
    {
        printf("%s internal with %s external\n",anotherarray[n], external_names[n]);
    }
}

最佳答案

static gchar *external_names;  //one dimensional array.

external_names 是一维数组,您正试图从二维数组internal_names 中分配它。

strcpy(external_names[n], internal_names[n]);

这是递增索引时发生的情况。

&internal_names[1] =  &internal_names[0] + size_t*100
&external_names[1] =  &external_names[0] + size_t*1

为了解决这个问题,

static gchar (*external_names)[100];

应该可以。

关于无法为文件变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14478232/

相关文章:

c++ - 用数字对字符串数组进行排序 C++

c - 对于函数指针 func_ptr,(*func_ptr)() 调用该函数。但是为什么 (****func_ptr)() 或 (***func_ptr)() 有效?

c - void** 指针和 void*[] 作为函数参数

c - 在 Swift 3 中使用具有回调函数指针作为参数的 C API 函数

javascript - 检查基于数组的复选框 - jQuery

c++ - 将 Window::set_title 与 gtkmm 一起使用时窗口标题截断

c++ - GTK+ 通过控件 id 查找 GTKWidget

python - 每秒更新 GTK+ MenuItem 倒计时,并定期调用函数

c - 非精确 STM32F303VC 定时器时钟

c - 删除未使用的变量会导致崩溃