0
I need to do an exercise that will basically ask the user to type two strings. Before that, an integer value X is entered representing the position from which the first string will be saved to the variable that stores it and only then the two strings are typed. Then a function places the second string at the position where the first string was received.
My doubts are:
1 - To create a separate file to call the function, do I need to use some library? I was giving error when I tried by myself;
2 - When using the function in the same main()
, I’m getting the
following warnings and the code is not running:
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]
passing argument 2 of 'trocarPosicao' makes pointer from integer without a cast [-Wint-conversion]
warning: passing argument 3 of 'trocarPosicao' makes pointer from integer without a cast [-Wint-conversion]
warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int'
Follows my code:
#include <stdio.h>
#include <stdlib.h>
void trocarPosicao (int posicao , char a[posicao] , char b[0]);
int main() {
int posicao;
char a[10] , b[10];
printf("Digite uma posição para o primeiro nome:");
fflush(stdout);
scanf("%d", &posicao);
printf("Digite um nome: ");
fflush(stdout);
scanf("%s" , &a[posicao]);
printf("Digite outro nome: ");
fflush(stdout);
scanf("%s" , b[0]);
trocarPosicao(posicao , a[posicao] , b[0]);
}
void trocarPosicao (int posicao , char a[posicao] , char b[0]) {
a[posicao] = b[0];
printf("Primeiro nome : %s", a[posicao]);
printf("Segundo nome: %s" , b[0]);
printf("Posição: %d ", posicao);
}
Are you sure it’s to put the second in the position of the first? It seems to be actually another problem: placing the second string in the position subsequent to the first one. Otherwise, it doesn’t make much didactic sense. It would be interesting to review the text.
– José
This is the question: Write a function that takes as parameter two strings, as well as a value integer that represents a position. The function must therefore insert the second string in at the given position of the first. Write a program that receives these two strings from the user, the desired position value , and call the previously implemented function and display the result to the user on the screen.
– Gabriel Poletti
What do you mean by "To create a separate file to call the function"? To call a function you don’t need any file. It is possible to define the source of a function in a different file and in the compilation process join the objects. Your setting of function parameters are strange. Maybe it should be: void trocarPosic (int position , char a[] , char b[]);
– anonimo
And how do I define the source of a function? because I saw some comments on the internet and needed to put a #include and the name of the file. And I’m pretty confused by that question too, it’s a work exercise, but it seems to be a little poorly worded
– Gabriel Poletti