2012-11-30 31 views
0

假設:struct foo_t { int X,Y,Z; }。某些函數需要一個數組struct foo_t併爲其設置一些值;是這樣的:複製結構問題

void foo(struct foo_t *f, size_t limit, size_t *result_length) 
{ 
    int i = 0; 
    struct foo_t a; 
    a.X = 5; 
    //... 
    struct foo_t b; 
    b.X = 10; 
    // ... 
    struct foo_t c; 
    c.X = 4; 
    //... 
    f[i++] = a; 
    f[i++] = b; 
    f[i++] = c; 
    *result_length = i; 
} 

然後:

struct foo_t buf[12]; 
struct foo_t positive[12]; 
struct foo_t negative[12]; 
size_t len; 
foo(buf, sizeof(buf)/sizeof(buf[0]), &len); 
int c,positive_len,negative_len; 
for(c = positive_len = negative_len = 0; c < len; c++) 
{ 
    if(buf[c].X < 8) 
     positive[positive_len++] = buf[c]; 
    else 
     negative[negative_len++] = buf[c]; 
} 

最後:

puts("POSITIVE:"); 
int i; 
for(i = 0; i < positive_len; i++) 
    printf("%d\n", positive[i].X); 
puts("NEGATIVE:"); 
for(i = 0; i < negative_len; i++) 
    printf("%d\n", nagative[i].X); 

的問題如下:而不是越來越"POSITIVE:\n4\n5""NEGATIVE:10"我越來越5 and 510不打印。換句話說,只有最後一個值被設置。這是爲什麼發生?我大大減少了我的代碼,試圖在這裏獲得一些幫助,因爲真正的功能大約有300行代碼,包括數據庫管理等等。如果真的需要,我會在這裏發佈。前使用=操作符,我會用memcpy()做結構的副本,我正/負陣列。

+0

難道當你使用'memcpy'前工作的呢? – Xymostech

+0

我不清楚你得到的輸出。它是'積極的:5 \ nNEGATIVE:5'? – philipvr

+0

@Xymostech: – Jack

回答

1

你在你的代碼一個錯字:

struct foo_t c; 
    b.X = 4; // this should be c.X = 4; 
    //... 
+0

謝謝。但是這個代碼只是一個例子。我在這裏的新線程的textarea上輸入了它。無論如何,我會解決它。 – Jack

+0

你能解決錯別字,並嘗試自己運行這些代碼嗎?除了這個錯字和錯字@JTA指出,我沒有看到任何錯誤。 – philipvr

1

你有兩個錯別字/蟲子在上面的例子:

  1. 您沒有設置爲C

    結構foo_t℃;

    b.X = 4;

  2. 在此printf的可變拼錯

    爲(I = 0;我< negative_len;我++)

    的printf( 「%d \ n」 個,性否定[I] .X);

+0

謝謝。但是這個代碼只是一個例子。我在這裏的新線程的textarea上輸入了它。無論如何,我會解決它。 – Jack

+0

有些時候應該是積極的。 –

1

有幾個錯誤。有些是拼寫,「c」在您的「foo」函數中從未分配過。

#include <stdlib.h> 
#include <stdio.h> 
#include <memory.h> 

typedef struct foo_t 
{ 
    int X, Y, Z; 
}foo_t; 

void foo(struct foo_t *f, size_t limit, size_t *result_length) 
{ 
    int i = 0; 
    struct foo_t a, b, c; 
    a.X = 5; 
    //... 
    b.X = 10; 
    // ... 
    c.X = 4; // CHANGE HERE FROM "B" to "C". 
    //... 
    f[i++] = a; 
    f[i++] = b; 
    f[i++] = c; 
    *result_length = i; 
} 

int main(int argc, char** argv) 
{ 

    // CORRECTED ALL SPELLING ERRORS!!! (POSITIVE/NEGATIVE) 
    struct foo_t buf[12]; 
    struct foo_t positive[12]; 
    struct foo_t negative[12]; 
    size_t len; 
    int c, positive_len, negative_len; 

    foo(buf, sizeof(buf)/sizeof(buf[0]), &len); 

    for(c = positive_len = negative_len = 0; c < len; c++) 
    { 
     if(buf[c].X < 8) 
      positive[positive_len++] = buf[c]; 
     else 
      negative[negative_len++] = buf[c]; 
    } 

    { // <-- IGNORE THIS BADNESS 
     int i; 
     puts("POSITIVE:"); 

     for(i = 0; i < positive_len; i++) 
      printf("%d\n", positive[i].X); 
     puts("NEGATIVE:"); 
     for(i = 0; i < negative_len; i++) 
      printf("%d\n", negative[i].X); 
    } 

    getchar(); 
} 
+0

我編輯我的帖子。另外,什麼是'memory.h'? – Jack

+0

@Jack你可以忽略它。我只是在粘貼代碼之前扔掉這些標題。 – Inisheer

1

這是你的代碼生成的SSCCE(Short, Self-Contained, Complete Example):

#include <stdio.h> 

struct foo_t { int X; }; 

static void foo(struct foo_t *f, size_t limit, size_t *result_length) 
{ 
    size_t i = 0; 
    struct foo_t a; 
    a.X = 5; 
    struct foo_t b; 
    b.X = 10; 
    struct foo_t c; 
    c.X = 4; 
    if (i < limit) 
     f[i++] = a; 
    if (i < limit) 
     f[i++] = b; 
    if (i < limit) 
     f[i++] = c; 
    *result_length = i; 
} 

int main(void) 
{ 
    struct foo_t buf[12]; 
    struct foo_t positive[12]; 
    struct foo_t negative[12]; 
    size_t len; 
    foo(buf, sizeof(buf)/sizeof(buf[0]), &len); 
    size_t c,positive_len,negative_len; 
    for (c = positive_len = negative_len = 0; c < len; c++) 
    { 
     if (buf[c].X < 8) 
      positive[positive_len++] = buf[c]; 
     else 
      negative[negative_len++] = buf[c]; 
    } 

    puts("POSITIVE:"); 
    for (size_t i = 0; i < positive_len; i++) 
     printf("%d\n", positive[i].X); 
    puts("NEGATIVE:"); 
    for (size_t i = 0; i < negative_len; i++) 
     printf("%d\n", negative[i].X); 
} 

它產生:

POSITIVE: 
5 
4 
NEGATIVE: 
10 

我不得不修復nagativenegativepostivepositive。我初始化了c.X。我使用limit確保沒有溢出(並修復警告)。我改變了各種int計數器變量size_t避免警告有關簽訂無符號VS比較。我從結構中消除了Y和Z成員,因爲它們在這個最小的例子中並沒有使用。

+0

我認爲這會更好,如果我張貼真實的代碼,而不是這個例子... – Jack

+2

@Jack:如果你要張貼代碼,張貼SSCCE。你是對的;您可能需要更準確地發佈您正在使用的內容,但您需要確保它是一個SSCCE(不是需要數據庫的300行)。 –