Search code examples
cturbo-c

Error C language


This code cannot convert char* to char**. I don't know what it means.

Here is my code:

#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();
}

The program is supposed to:

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

Solution

  • &s means char (*)[30](pointer to array of char[30]) not char *[] (array of pointer to char)

    For example, It modified as follows.

    #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;
    }