#include "stdafx.h"
#include <stdio.h>
struct s
{
char *st;
struct s *sp;
};
struct s *p1,*p2;
void swap(struct s *p1,struct s *p2);
int main()
{
int i;
struct s *p[3];
static struct s a[]={
{"abc",a+1},{"def",a+2},{"ghi",a}
};
for(i=0;i<3;i++)
{
p[i]=a[i].sp;
}
swap(*p,a);
printf("%s %s %s\n",p[0]->st,(*p)->st,(*p)->sp->st);
return 0;
}
void swap(struct s *p1,struct s *p2)
{
char *temp;
temp = p1->st;
p1->st = p2->st;
p2->st = temp;
}
このプログラムは、abc、abc、ghiとして出力します。私の疑問は、p [0] - > st、(* p) - > st、(* p) - > sp-> st outputs.weはabcまたはghiで初期化されていません。cの文字列と構造
p [0]と* pは本質的に同じものです。 –
あなたは 'st'を初期化していないのですか? –