Questions about the use of C functions

Asked

Viewed 221 times

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.

  • 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.

  • 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[]);

  • 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

1 answer

0


I don’t know if I understood this exercise right, but most of the errors in your code refer to the manipulation of arrays.

Variables used to store strings a and b, are arrays of char. The value contained in a and b is a pointer to the first position of these arrays, a and b are pointers but the values contained in a[0], a[1], a[2] are of the type char.

When you do something like scanf("%s", b[0]), you are telling the program to put the user input, which is an array of characters, in the index 0 of b, but the index 0 of b contains only one character, as a single char will store an array of char?

On the other hand when you use scanf("%s", &a[posicao]), you are passing the memory address of a[posicao] to the scanf, this memory position is of the type char*. This is valid, but what is the meaning of this code? You will store the user input from the position posicao array. The stored word is the same, but now you have the garbage that was not initialized from the beginning of the array, and for what purpose?

To receive user input has no tricks, only

int main() 
{
    int posicao;
    char palavra_a[10], palavra_b[10];

    printf("Digite um nome: ");
    scanf("%s9", palavra_a);

    printf("Digite outro nome: ");
    scanf("%s9", palavra_b);

    printf("Digite uma posição: ");
    scanf("%d", &posicao);
}

Now to insert palavra_b inside palavra_a, we’ll have to play a little with the positions of memory:

void inserir(int posicao, char *palavra_a, char *palavra_b) 
{
    //messo o tamanho das palavras
    size_t tam_a = strlen(palavra_a);
    size_t tam_b = strlen(palavra_b);
    //crio um array com tamanho capaz de armazenar palavra a e b
    char nova_palavra[tam_a + tam_b + 1];

    //se a posição de inserção não for válida, atribuo um novo valor para posicao
    if (posicao > tam_a)
        posicao = tam_a;
    if (posicao < 0)
        posicao = 0;

    //copio a palavra_a para nova_palavra
    strcpy(nova_palavra, palavra_a);
    //copio a palavra_b para nova_palavra, a partir da posição de memória passado
    strcpy(&nova_palavra[posicao], palavra_b);
    //copio a parte de palavra_a que foi sobrescrevia para a posição de memória no final de nova_palavra
    strcpy(&nova_palavra[posicao + tam_b], &palavra_a[posicao]);

    printf("%s", nova_palavra);
}

Or using loops instead of strcpy

//copio palavra_a até posicao
for (i = 0; i < posicao; ++i)
    nova_palavra[i] = palavra_a[i];

//copio palavra_b e insiro em nova_palavra a partir de posicao
for (i = 0; i < tam_b; ++i)
    nova_palavra[posicao + i] = palavra_b[i];

//copio o restante de palavra_a e insiro a partir de posicao + tamanho da palavra_b
for (i = posicao; i < tam_a; ++i)
    nova_palavra[i + tam_b] = palavra_a[i];

//Adiciono o null terminator
nova_palavra[tam_a + tam_b] = '\0';

To call the function, simply inserir(posicao, palavra_a, palavra_b), do not need to send (and should not) positions from your array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.