[英]C: 2d char pointer in a struct
I want to write and print some strings in a 2d array in a struct. 我想在结构的2d数组中编写和打印一些字符串。 The struct is called Router and it is in a header file, the 2d array is defined in that struct and it's called **addTab.
该结构称为Router,位于头文件中,该结构中定义了2d数组,其名称为** addTab。 When I try to print one line of the array using the function viewTable the program stopped working... why?
当我尝试使用功能viewTable打印阵列的一行时,程序停止工作...为什么?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "router.h"
#define N 8
#define M 3
#define sizeMess 5
Router *createRouter(){
Router *NewRouter=(Router*)malloc(sizeof(Router));
int i;
NewRouter->addTab=(char **) malloc(N*sizeof(char *));
for(i=0;i<N;i++)
NewRouter->addTab[i]=(char *) malloc(M*sizeof(char));
return NewRouter;
}
void viewTable(Router *r, int a){
int i,j;
for(i=0;i<N;i++){
for(j=0;j<M;j++){
printf("raw\t col\t address\t value\t\n");
printf("%d\t %d\t",i,j);
printf("%p\t",&r->addTab[i][j]);
printf("%s\t\n",r->addTab[i][j]);
}
}
}
void updateTable(Router *r, int conn, char *addr1, char *addr2){
r->addTab[conn][1]=addr1;
r->addTab[conn][2]=addr2;
}
First off: Don't cast the result of malloc . 首先, 不要转换malloc的结果 。
Assuming that you want to store char*
pointers in your 2D array (as the title of your question says), you will need to define it as char ***
in your Router
structure, like this: 假设要在2D数组中存储
char*
指针(如问题标题所示),则需要在Router
结构中将其定义为char ***
,如下所示:
typedef struct router {
char ***addTab;
} Router;
Next, you will need to change your createRouter
function, so that it can store an array of char*
pointers, instead of a single byte for each element, like so: 接下来,您将需要更改
createRouter
函数,以便它可以存储char*
指针数组,而不是每个元素存储一个字节,如下所示:
Router *createRouter(){
Router *NewRouter=malloc(sizeof(Router));
int i;
NewRouter->addTab=malloc(N*sizeof(char **));
for (i=0;i<N;i++)
NewRouter->addTab[i]=malloc(M*sizeof(char *));
return NewRouter;
}
I'm not sure how you call your updateTable
function, but unless you actually fill up the entire array with char*
pointers, your viewTable
function will also invoke Undefined Behavior , because the printf
statements will attempt to access uninitialized data. 我不确定如何调用
updateTable
函数,但是除非实际使用char*
指针填充整个数组, viewTable
函数还将调用Undefined Behavior ,因为printf
语句将尝试访问未初始化的数据。 You could avoid this by using calloc
instead of malloc
when allocating the 2D array (or explicitly memset
it), and then adding NULL checks in your viewTable
function. 您可以通过使用避免这种
calloc
代替malloc
分配时,二维数组(或明确memset
话),然后在你的加入null检查viewTable
功能。
Finally, if you're calling updateTable
with char*
pointers that are not string literals or they have not been allocated on the heap, you might have further issues... 最后,如果您使用非字符串文字的
char*
指针或没有在堆上分配的char*
指针调用updateTable
,则可能会遇到其他问题...
Your updateTable() doesn't work as you'd expect. 您的updateTable()无法正常工作。 You allocated memory in
r->addTab[i][j]
and, afterwards, assign a pointer to it ( r->addTab[conn][1]=addr1
). 您在
r->addTab[i][j]
分配了内存,然后分配了指向它的指针( r->addTab[conn][1]=addr1
)。 On access in viewTable, the program tries to read the memory at addr1
, but most likely won't be able to read it, thus crashes. 在访问viewTable时,程序尝试读取
addr1
处的内存,但很可能无法读取它,从而导致崩溃。
Use a function to copy a given string to r->addTab, eg like so: 使用函数将给定的字符串复制到r-> addTab,例如:
void router_tab_printf(Router *r, const int conn, const int i, const char *value) {
sprintf(r->addTab[conn][i], "%s", value);
}
This assumes that r->addTab[conn][i]
is large enough to hold value
. 假设
r->addTab[conn][i]
足够大,可以容纳value
。
you need to change your updateTable 您需要更改您的updateTable
void updateTable(Router *r, int conn, char *addr1, char *addr2){
strcpy(r->addTab[conn], addr1);
strcpy(r->addTab[conn+1 /*or something*/], addr2);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.