此代码无法将char*
转换为char**
。我不知道这意味着什么。
这是我的代码:
#include <stdio.h>
#include <conio.h>
#include <string.h>
shift( char *s[] , int k )
{
int i,j;
char temp[50];
for( i = 0 ; i < k ; i++ )
temp[i]=*s[i] ;
for( j = 0 ; j < strlen(*s) ; j++ )
{
*s[j] = *s[k] ;
k++ ;
}
strcpy(*s,temp);
}
main()
{
int i,j=0,k;
char s[30];
printf("please enter first name ");
gets(s);
scanf("%d",&k);
shift( &s , k);
puts(s);
getch();
}
该计划应该:
读取字符串S1和索引'K',然后调用自己的函数来旋转字符串 输入的索引。程序的输出应如下:
Enter your string: AB3CD55RTYU Enter the index of the element that rotates the string around: 4 The entered string: AB3CD55RTYU Enter the element that rotates the string around: D The rotated string is : D55RTYUAB3C
答案 0 :(得分:0)
&s
表示char (*)[30]
(指向char [30]数组的指针)而不是char *[]
(指向char的指针数组)
例如,它修改如下。
#include <stdio.h>
#include <conio.h>
#include <string.h>
void shift(char s[],int k){
int i, len;
char temp[50];
for(i=0;i<k;i++)
temp[i]=s[i];
temp[i] = '\0';
len = strlen(s);
for(i=0;k<len;i++)
s[i]=s[k++];
strcpy(&s[i],temp);
}
int main(){
int k;
char s[30];
printf("please enter first name ");
gets(s);
scanf("%d", &k);
shift(s , k);
puts(s);
getch();
return 0;
}
答案 1 :(得分:0)
使用结构的示例(执行复制)。但是,这是浪费资源。
#include <stdio.h>
#include <conio.h>
typedef struct word {
char str[30];
} Word;
Word shift(Word word, int k){
Word temp;
int i = 0, j;
for(j=k;word.str[j]!='\0';++j)
temp.str[i++]=word.str[j];
for(j=0;j<k;++j)
temp.str[i++]=word.str[j];
temp.str[i] = '\0';
return temp;
}
int main(){
int k;
Word w;
printf("please enter first name ");
gets(w.str);
scanf("%d", &k);
w=shift(w , k);
puts(w.str);
getch();
return 0;
}
答案 2 :(得分:0)
shift(char* s[],int k); //shift expects char**; remember s[] is actually a pointer
main()
{
char s[30]; // when you declare it like this s is a pointer.
...
shift(s , k);
}
您应该将shift函数签名更改为shift(char* s,int k);
,因为您并不真正需要指向指针的指针。你只需要传递数组的开头。