我正在尝试将输入读入2dim char结构成员
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { HIGHT=14, WIDTH=147, IMAGES=24 };
typedef struct{
char *frame[HIGHT][WIDTH];
int fps;
} frame_stack_t;
void out(frame_stack_t *frame_stack[IMAGES]);
int main(){
frame_stack_t *frame_stack[IMAGES];
for (int i=0; i<IMAGES; i++){
for (int j=0; j<HIGHT; j++){
strcpy( frame_stack[i]->frame[j], "some text" );
}
}
out(frame_stack);
}
void out(frame_stack_t *frame_stack[IMAGES]){
for (int i=0; i<IMAGES; i++){
for (int j=0; j<HIGHT; j++){
printf("%s",frame_stack[i]->frame[j]);
}
}
}
看起来对我来说,但我记得以下输出:
test_struct.c: In function ‘main’:
test_struct.c:19:25: warning: passing argument 2 of ‘strcpy’ from incompatible pointer type [-Wincompatible-pointer-types]
strcpy( "some text", frame_stack[i]->frame[j] );
^
In file included from test_struct.c:3:0:
/usr/include/string.h:125:14: note: expected ‘const char * restrict’ but argument is of type ‘char **’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
test_struct.c: In function ‘out’:
test_struct.c:29:11: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Wformat=]
printf("%s",frame_stack[i]->frame[j]);
^
Speicherzugriffsfehler
gdb告诉我strcpy失败
Program received signal SIGSEGV, Segmentation fault.
__strcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:546
546 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: Datei oder Verzeichnis nicht gefunden.
有人可以告诉我这里有什么不对吗?
答案 0 :(得分:0)
有问题的C代码存在很多问题。请在发布问题之前做一些阅读和练习,打开这样的论坛。请参阅以下更正的代码并尝试了解:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { HIGHT=14, IMAGES=24, WIDTH=147 };
typedef struct{
char frame[HIGHT][WIDTH];
int fps;
} frame_stack_t;
void out(frame_stack_t *frame_stack);
int main()
{
frame_stack_t frame_stack[IMAGES];
int i, j;
for (i=0; i<IMAGES; i++)
{
for (j=0; j<HIGHT; j++)
{
strcpy(frame_stack[i].frame[j], "some text\n" );
}
}
out(frame_stack);
}
void out(frame_stack_t frame_stack[])
{
int i,j;
for (i=0; i<IMAGES; i++)
{
for (j=0; j<HIGHT; j++)
{
printf("%s",frame_stack[i].frame[j]);
}
}
}