开发者

Why the swap program which collects addresses in pointers to pointers did not work?

I have a program below

void swap(char **s1,char **s2);

int main()
{
 char *list[] = {
       "Das",
       "Kannan",
       "Rajendran",
       "Shahul"
 };   



 printf("Before swap list[0] = %s,list[1] = %s\n",*list[0],*list[1]);

 swap(&list[0],&list[1]);

 printf("After swap list[0] = %s,list[1] = %s\n",*list[0],*list[1]);

 return 0;

}

void swap(char **s1,char **s2)
{
 char *t;

 t = *s1;
 *s1 = *开发者_StackOverflow中文版s2;
 *s2 = t;
}

I am trying to swap the addresses of list[0] and list[1].

Visual Studio 2008 is generating an error while running(Start debugging) this program. The error generated was

Unhandled exception at 0x1029984f (msvcr90d.dll) in ConsoleApp.exe: 0xC0000005: Access violation reading location 0x00000044.

No compilation errors.

May I know why pointer to pointer used doesn't work properly. Also want to know why void swap(char *s1,char *s2) also didn't work.


Visual Studio 2008 is generating an error while running (Start debugging) this program.

Your printouts are faulty. Remove the asterisks:

printf("Before swap list[0] = %s,list[1] = %s\n",list[0],list[1]);

swap(&list[0],&list[1]);

printf("After swap list[0] = %s,list[1] = %s\n",list[0],list[1]);

Why? Well, list[0] is the string "Das" which has type char *. That's what you want to pass to printf. If you dereference that pointer with an asterisk you end up passing the first character 'D' to printf, where printf is expecting a char * string. It ends up trying to treat the character 'D' as a pointer. The ASCII value of 'D' is 68, or 0x44 in hex, so that explains the error message you got.

Also want to know why void swap(char *s1,char *s2) also didn't work.

With that function you'd be able to swap the characters in two strings, but you wouldn't be able to swap the strings themselves. Think of the swap function as needing pointers to the objects being swapped. If you were swapping two integers, you'd have swap(int *i1, int *i2). You want to swap two strings of type char *, which means the swap function needs two stars: swap(char **s1, char **s2). Does that make sense?


The problem is in your printf statement. You are de-referencing your string pointer. Change your printf from:

printf("Before swap list[0] = %s,list[1] = %s\n",*list[0],*list[1]);

to:

printf("Before swap list[0] = %s,list[1] = %s\n",list[0],list[1]);

(and the after swap one as well) and all will be good.


printf("Before swap list[0] = %s,list[1] = %s\n",*list[0],*list[1]);
swap(&list[0],&list[1]);
printf("After swap list[0] = %s,list[1] = %s\n",*list[0],*list[1]);

You don't need to dereference these char * here. Instead, you should do:

printf("Before swap list[0] = %s,list[1] = %s\n",list[0],list[1]);
swap(&list[0],&list[1]);
printf("After swap list[0] = %s,list[1] = %s\n",list[0],list[1]);

Your list array contains char * elements, where the char * elements are pointers to C strings (null terminated sequences of characters). By dereferencing it, you are actually passing the first character of each string into printf, instead of the string itself. When printf itself tries to dereference that character value, it causes it to try to access an invalid segment of memory, leading to a segmentation fault.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜