2014-11-30 2 views
-6

Эта программа используется для замены двух строк и я не использую таНос и делать это с помощью функции, и это дает мне ошибку, что это неполный тип не допускаетсяНеполное типа не допускается в программе обмена

#include<stdio.h> 
#include<conio.h> 
#include<string.h> 
void swapping(char s1[],char s2[]) 
{ 
int temp=s1; 
s1=s2; 
s2=temp; 
} 

int main (void) 
{ 
char st1[30],st2[30]; 
printf("Enter the first string"); 
scanf("%s",&st1); 
printf("Enter the second string"); 
scanf("%s",&st2); 
printf("The new string after swapping ",swapping(st1,st2)); 
getch(); 
} 
+1

Перемещение 30 элементов одновременно? –

+6

Пожалуйста, уделите время изучению основ C или C++. – juanchopanza

+0

, чтобы вы помогли? –

ответ

0

Программа ниже делает то, что вы хотите:

#include<stdio.h> 
#include<conio.h> 
#include<string.h> 
void swapping(char s1[],char s2[]) 
{ 
char temp[30]; //create an array of char in order to store a string 

strcpy(temp,s1); 
strcpy(s1,s2); 
strcpy(s2,temp); //use strcpy to swap strings 
} 

int main (void) 
{ 
char st1[30],st2[30]; 

printf("Enter the first string"); 
scanf("%s",st1); 
printf("Enter the second string"); 
scanf("%s",st2); //the & is removed from both the scanfs 

printf("The first string is %s and the second string is %s \n",st1,st2); //print before swapping 
swapping(st1,st2); //swap strings 

printf("The first string is %s and the second string is %s \n",st1,st2); //print after swapping 

getch(); 
return 0; 
} 

Вы можете удалить printf, который печатает st1 и st2 перед сменой, если вы не хотите. Удаляется &, так как имя массива распадается на точку er до его первого элемента. Вам также понадобится другой массив char вместо int, чтобы обменять обе строки.

1

В это определение функции

void swapping(char s1[],char s2[]) 
{ 
int temp=s1; 
s1=s2; 
s2=temp; 
} 

переменная s1 имеет тип char * в то время как переменная temp имеет тип int. Компилятор не может сделать инициализацию temp в декларации

int temp=s1; 

без литья s1 к типу Int. Но если вы добавите кастинг, функция не имеет смысла.

Учитывайте, что в массивах нет оператора присваивания.

Если ваш компилятор поддерживает массивы переменной длины, то вы можете написать

#include <stdio.h> 
#include <string.h> 

void swapping(size_t n, char s1[n], char s2[n]) 
{ 
    char tmp[n]; 

    strcpy(tmp, s1); 
    strcpy(s1, s2); 
    strcpy(s2, tmp); 
} 

int main(void) 
{ 
    char s1[30] = "Hello"; 
    char s2[30] = "Bye-bye"; 

    printf("%s\t%s\n", s1, s2); 

    swapping(30, s1, s2); 

    printf("%s\t%s\n", s1, s2); 

    return 0; 
} 

В противном случае функция может выглядеть, как показано ниже

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

void swapping(char s1[], char s2[], size_t n) 
{ 
    char *tmp = malloc(n * sizeof(char)); 

    strcpy(tmp, s1); 
    strcpy(s1, s2); 
    strcpy(s2, tmp); 

    free(tmp); 
} 

int main(void) 
{ 

    char s1[30] = "Hello"; 
    char s2[30] = "Bye-bye"; 

    printf("%s\t%s\n", s1, s2); 

    swapping(s1, s2, 30); 

    printf("%s\t%s\n", s1, s2); 


    return 0; 
} 

В обоих случаях выход

Hello Bye-bye 
Bye-bye Hello