鱼C论坛

 找回密码
 立即注册
查看: 2969|回复: 6

有关strcpy函数的问题

[复制链接]
发表于 2019-10-15 17:59:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <string.h>
  2. #include <stdio.h>
  3. void main()
  4. {
  5.      int i,j,c;
  6.      char p[10];
  7.      char *arr1[3]={"123","321","213"};
  8.      char **arr=arr1;   
  9.      for(i=0;i<2;i++)
  10.      {
  11.           for(j=0;j<2-i;j++)
  12.           {
  13.               if(  (c=strcmp( *(arr+j),*(arr+j+1) )) <0)
  14.               {
  15.                   strcpy(p,*(arr+j));   /*这里能复制过去
  16.                   strcpy(*(arr+j),*(arr+j+1));  /*这里为什么不行
  17.                   strcpy(*(arr+j+1),p);

  18.               }
  19.           }
  20.      }
  21.      printf("%s %s %s",*(arr+0),*(arr+1),*(arr+2));
  22.      
  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-10-16 10:59:58 | 显示全部楼层
本帖最后由 xypmyp 于 2019-10-16 11:08 编辑

The problem related to how pointer works in 2D array.

  1. /*
  2.         int i, j, c;
  3.         char arr[3][4] = {
  4.                 {"123"},
  5.                 {"321"},
  6.                 {"213"}
  7.         };

  8.         char* pointerTo2DArray = &arr[0][0];
  9. */

  10.         int i, j, c;
  11.         char p[10];
  12.         char *arr1[3] = { "123","321","213" };
  13.         char **arr = arr1;
  14.        
  15.         strcpy((char*)((int*)arr +0), (char*)((int*)arr + 1));

  16.         printf("%s %s %s", (char*)*((int*)arr + 0), (char*)*((int*)arr + 1),(char*)*((int*)arr + 2));
  17.         getchar();
  18.         return 0;
复制代码


As the lever 1 address is pointing to a memory address which 4 Byte long = sizeof(int), than you convert it to (char*) which strcpy() required [optional, it's the start address of your string].

Memory in realtime

Memory in realtime

Output

Output
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-16 22:34:30 | 显示全部楼层
xypmyp 发表于 2019-10-16 10:59
The problem related to how pointer works in 2D array.

虽然看不大懂,但还是要谢谢你
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-17 10:46:42 | 显示全部楼层
朋乌龟 发表于 2019-10-16 22:34
虽然看不大懂,但还是要谢谢你

Which part you don't understand?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-17 16:23:37 | 显示全部楼层
xypmyp 发表于 2019-10-17 10:46
Which part you don't understand?

我可以交换首地址来交换位置,不需要strcpy(),
我理解的是:*(arr+0)这是整形指针?需要把它转换成字符型??
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-17 21:02:52 | 显示全部楼层
Yes, as arr is a type of (char**), you have to convert it to (char*)

*(arr+0) is not a (int*) pointer, it's a (char**) pointer which you declare at the beginning,
so you have to convert it to (int*) for pointing to an valid address. which is the start address of the string.
then, you have to convert it to (char*), as the function strcpy() needs a (char*) pointer.

Try +1 or -1 in between, you may understand what i'm saying
printf("%s",((char*)*((int*)arr + 0) +1));
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-18 11:17:06 | 显示全部楼层
为什么我一定要将其转换为(int *)才能指向有效地址
我这样也可以,对吗?
  1. int i, j, c;
  2.         char p[10];
  3.         char *arr1[3] = { "123","321","213" };
  4.         char **arr = arr1;
  5.         
  6.         strcpy((arr +0), (arr + 1));

  7.         printf("%s %s %s", *(arr + 0), *(arr + 1),*(arr + 2));
  8.         getchar();
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 07:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表