鱼C论坛

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

有关strcpy函数的问题

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

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

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

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

              }
          }
     }
     printf("%s %s %s",*(arr+0),*(arr+1),*(arr+2));
     
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

The problem related to how pointer works in 2D array.
/*
        int i, j, c;
        char arr[3][4] = { 
                {"123"},
                {"321"},
                {"213"}
        };

        char* pointerTo2DArray = &arr[0][0];
*/

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

        printf("%s %s %s", (char*)*((int*)arr + 0), (char*)*((int*)arr + 1),(char*)*((int*)arr + 2));
        getchar();
        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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

虽然看不大懂,但还是要谢谢你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

Which part you don't understand?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

我可以交换首地址来交换位置,不需要strcpy(),
我理解的是:*(arr+0)这是整形指针?需要把它转换成字符型??
想知道小甲鱼最近在做啥?请访问 -> 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));
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

        printf("%s %s %s", *(arr + 0), *(arr + 1),*(arr + 2));
        getchar();
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-4 15:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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