2015-10-11 80 views
-2

這是我的代碼;我想交換下面的名字。我在練習這個;在C中交換雙指針

我得到的錯誤是

Segmentation fault 

任何援助將不勝感激。

void nameSwap(char **wife[3],char **husband[3]) 
{ 
    int i; 
    char **tmp[3]; 
for(i=0;i<3;i++) 
{ 

    *tmp[i]=*wife[i]; 
    *wife[i] = *husband[i]; 
    *husband[i] = *tmp[i]; 
} 

int main(int argc,char *argv[]) 
{ 
char *name1[3]={"Chicago","University","Computer"}; 
char *name2[3]={"I","Love","Uchicago"}; 

int k; 
char **p1[3]; 
char **p2[3]; 

for(k=0;k<3;k++) 
{ 
*p1[k]=name1[k]; 
*p2[k]=name2[k]; 
} 

for(k=0;k<3;k++) 
{ 
printf("%s %s\n",*p1[k],*p2[k]); 
} 

nameSwap(&p1[3],&p2[3]); 

for(k=0;k<3;k++) 
{ 
printf("%s %s\n",*p1[k],*p2[k]); 
} 
return 0; 

} 
+1

你應該多增加一些星星。試試成爲一名六星級程序員。 – Olaf

+0

@Mia'char ** p1 [3];'表示'char **'的數組。 – BLUEPIXY

+0

請正確縮進你的代碼 – chqrlie

回答

0

雙指針指的char **

#include <stdio.h> 

void nameSwap(char ***wife, char ***husband){ 
    char **tmp; 

    tmp = *wife; 
    *wife = *husband; 
    *husband = tmp; 
} 

int main(void){ 
    char *name1[3]={"Chicago","University","Computer"}; 
    char *name2[3]={"I","Love","Uchicago"}; 

    int k; 
    char **p1; 
    char **p2; 

    p1 = name1; 
    p2 = name2; 

    for(k=0;k<3;k++){ 
     printf("%s %s\n", p1[k], p2[k]); 
    } 

    nameSwap(&p1, &p2); 

    for(k=0;k<3;k++){ 
     printf("%s %s\n", p1[k], p2[k]); 
    } 
    return 0; 
} 

指針的情況下

情況下char *var_name[3]

#include <stdio.h> 

void nameSwap(char *(**wife)[3], char *(**husband)[3]){ 
    char *(*tmp)[3]; 

    tmp = *wife; 
    *wife = *husband; 
    *husband = tmp; 
} 

int main(void){ 
    char *name1[3]={"Chicago","University","Computer"}; 
    char *name2[3]={"I","Love","Uchicago"}; 

    int k; 
    char *(*p1)[3]; 
    char *(*p2)[3]; 

    p1 = &name1; 
    p2 = &name2; 

    for(k=0;k<3;k++){ 
     printf("%s %s\n", (*p1)[k], (*p2)[k]); 
    } 

    nameSwap(&p1, &p2); 

    for(k=0;k<3;k++){ 
     printf("%s %s\n", (*p1)[k], (*p2)[k]); 
    } 
    return 0; 
} 
+0

感謝您的幫助。高度讚賞。 –

1

讓我們開始與交換功能:它需要指針2個陣列3個指針char。它可以被聲明爲void nameSwap(char *wife[3], char *husband[3]),但請注意,類似數組的語法具有誤導性,實際上等同於:void nameSwap(char *wife[], char *husband[])或此:void nameSwap(char **wife, char **husband)。在C中,指向char的指針數組作爲指針傳遞給指向char的指針。

爲了執行交換,只需要一個臨時變量,因爲一次只交換2個指針。

這裏是一個修正版本,與[]語法強調一個事實,即wifehusband點陣列,而不是單一指針(就像在mainargv):

void nameSwap(char *wife[], char *husband[]) 
{ 
    for (int i = 0; i < 3; i++) { 
     char *tmp = wife[i]; 
     wife[i] = husband[i]; 
     husband[i] = tmp; 
    } 
} 

然後讓我們看看main功能:

int main(int argc, char *argv[]) 
{ 
    // These should really be const char *, but this is a minor problem 
    char *name1[3] = { "Chicago", "University", "Computer" }; 
    char *name2[3] = { "I", "Love", "Uchicago" }; 

    int k; 
    char *p1[3]; // removed the extra *. These are just arrays of pointers 
    char *p2[3]; 

    for (k = 0; k < 3; k++) { 
     p1[k] = name1[k]; // removed the extra *, just assign array elements 
     p2[k] = name2[k]; 
    } 

    for (k = 0; k < 3; k++) { 
     printf("%s %s\n", p1[k], p2[k]); // pass the pointers 
    } 

    nameSwap(p1, p2); // simple pass the arrays 
    // the nameSwap function receives pointers to the arrays, 
    // just as if called as nameSwap(&p1[0], &p2[0]);   

    for (k = 0; k < 3; k++) { 
     printf("%s %s\n", p1[k], p2[k]); // should work now. 
    } 

    return 0; 
} 

我希望這個解釋能幫助你更好地理解。 C語言中掌握的指針和數組是。最好的方法是騎自行車:從已經瞭解藝術的朋友處獲得一些幫助。

+0

感謝您的幫助。高度讚賞。 –