2017-05-23 17 views
-1

我試圖讀取輸入到2dim炭結構成員,的C 2維字符在結構

#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]); 
     } 
    } 
} 

是正確的看向我,但我recive以下的輸出:

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. 

有人可以告訴我什麼是錯的嗎?

+1

您有一個char指針數組的數組。對數組進行索引會給你一個char指針數組,而不是char指針 –

+1

請在提問之前至少做一些手動讀取。例如,你的'strcpy()'參數是圍繞着 –

+0

thx錯誤的方式再次混合,我發佈了新的錯誤。 – Bubblepop

回答

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]); 
    } 
} 
} 
+0

你的意思是哪個問題?旁邊描述的問題我發佈的代碼運行良好。 – Bubblepop