c - C语言错误

标签 c turbo-c

此代码无法将 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();
}

该程序应该:

read string S1 and index ‘K’, then call your own function that rotates the string around the entered index. The output of your program should be as follows:

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

最佳答案

&s 表示 char (*)[30](指向 char[30] 数组的指针),而不是 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;
}

关于c - C语言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20324332/

相关文章:

c - 制作 : *** [] Error 1

c - c 中 "signed short "数据类型有多少字节?

c - 如果 getaddrinfo 失败一次,它将永远失败(即使在网络准备就绪之后)

c - Turbo C 中的 SHA256 算法无法编译

任何人都可以建议一种无需等待按键即可捕获击键的编码方法吗?

c - 添加闪烁颜色但它不闪烁

objective-c - 模数运算符目标不起作用?

c++ - C memcmp 第三个参数类型

c - 边界填充算法中途停止填充

c - C 中的三元条件运算符