关于几个字符串的表达str,&str,&str【0】的问题

C语言 码拜 9年前 (2015-05-11) 749次浏览 0个评论
 

#include <stdio.h>
#include <ctype.h>
#include <string.h>
void removeLeadingUppercase(char str[]) {
  while (1) {
    if (str[0] == “”\0″” || !isupper(str[0]))
return;
    strcpy(&str[0], &str[1]);
} }
int main() {
  char s[] = “ABCDEFabcdef”;
  removeLeadingUppercase(s);
  printf(“%s\n”, s);
}
这程序的作用是把字符串中的大写字母去掉。我不理解为什么strcpy函数不是复制吗?怎样可以把大写字母去掉?
然后strcpy(&str[0], &str[1]);
strcpy(str, &str[1]);
strcpy(str, str + 1);
三种表达都是正确且相等的吗?
如果是相等的,为什么&str【0】和str能识别出指的是第一个字符的地址而不是整个字符串的地址?
谢谢

10分
三种表达相等
理解strcpy的含义……
10分
谁写的破代码

引用

char *strcpy( char *dest, const char *src );
Copies the character string pointed to by src, including the null terminator to the character array whose first element is pointed to by dest. The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap.

引用 2 楼 iyomumx 的回复:

谁写的破代码

引用

char *strcpy( char *dest, const char *src );
Copies the character string pointed to by src, including the null terminator to the character array whose first element is pointed to by dest. The behavior is undefined if the dest array is not large enough. The behavior is undefined if the strings overlap.

代碼是教科書上的…汗…
上面那段strcpy的英文是說strcpy(&str[0], &str[1]);裡的兩個參數輸入的&str[0]和&str[1]是兩個指針,然後strcpy運行時是對兩個指針所指向的字符串進行操作的意思?

還是不能理解怎麼通過strcpy(&str[0],&str[1])的複製,來去掉大寫字母的str[0]……
10分
引用 4 楼 qq_27872797 的回复:

還是不能理解怎麼通過strcpy(&str[0],&str[1])的複製,來去掉大寫字母的str[0]……

把从&str [1]开始的串(自然不含首字符了)复制到str处,即除首字符外全部字符左移一个位置,首字符自然没了

10分
引用 5 楼 cao_julians 的回复:
Quote: 引用 4 楼 qq_27872797 的回复:

還是不能理解怎麼通過strcpy(&str[0],&str[1])的複製,來去掉大寫字母的str[0]……

把从&str [1]开始的串(自然不含首字符了)复制到str处,即除首字符外全部字符左移一个位置,首字符自然没了

+1


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于几个字符串的表达str,&str,&str【0】的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!