Pointer to char in printf () does not work

1

I decided to make this code simple, where I have an empty vector and a function that fills it only with a letter that the user types, but I can not print.

If I use that commented% com, it works, but if I print out it does not come out of the blue. And if I try to print it in the printf() function, it exits with m 2 at the end. Why?

#include <stdio.h>
#include <string.h>
#define TAM 3

void preencher(char *str, char letra){
    int c;

    for (c=0; c<TAM; c++){
        *str = letra;
        //printf("%c", *str);
        *str++;
    }

    while (*str) printf("%c", str++);
}

int main(){
    char str[TAM], letra;

    scanf("%c", &letra);

    preencher(&str, letra);
}
    
asked by anonymous 26.07.2018 / 03:17

3 answers

4

You have several errors in this code. You are passing an array by reference, where array is already a reference.

If you are going to use a array variable, use it instead of accessing the direct pointer. If you are going to access the pointer, do not create a variable. But I would create the variable because walking through the array without copying its address will make the original note change position and will have subsequent problems, which is the main problem it can not use later.

You're not putting the terminator that every string needs (I imagine you're wanting create a string .

To print or do anything other than call the print function as string . If you want to do it in the hand, it's the same fill code, but it does the printing.

How it works:

#include <stdio.h>
#define TAM 3

void preencher(char *str, char letra) {
    for (int c = 0; c < TAM - 1; c++) str[c] = letra;
    str[TAM - 1] = 0;
    printf("%s", str);
}

int main(){
    char str[TAM], letra;
    scanf("%c", &letra);
    preencher(str, letra);
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

You have better ways of doing iso.

    
26.07.2018 / 04:00
2

@Maniero has already indicated the various errors he had, but I think it is important to mention that he should always be aware of what he is trying to do, because in many cases there are already functions ready for it.

In case you already include <string.h> , then you can use the memset function that allows you assign a value to a byte-by-byte memory zone. If you are assigning in a char vector will work as expected.

Example:

#include <stdio.h>
#include <string.h>
#define TAM 3

int main(){
    char str[TAM + 1]; //+1 para o terminador
    char letra;
    scanf("%c", &letra);
    memset(str, letra, TAM); //colocar a letra repetidamente, igual ao preencher que tinha
    str[TAM] = '
#include <stdio.h>
#include <string.h>
#define TAM 3

int main(){
    char str[TAM + 1]; //+1 para o terminador
    char letra;
    scanf("%c", &letra);
    memset(str, letra, TAM); //colocar a letra repetidamente, igual ao preencher que tinha
    str[TAM] = '%pre%'; //colocar o terminador no fim da string
    printf("%s", str); //mostrar
}
'; //colocar o terminador no fim da string printf("%s", str); //mostrar }

Note that I used the string size as TAM + 1 and put the terminator in TAM , effectively getting TAM written letters. Alternatively you can only allocate TAM and put the terminator in TAM - 1 (as did @Many), getting TAM - 1 written letters.

View the code on Ideone

    
26.07.2018 / 11:38
0

If you want to print a vector of characters instead of a string, you can do the following:

#include <stdio.h>
#include <string.h>
#define TAM 3

void preencher(char *str,char letra){

int c;

printf("Conteudo do vetor: ");

 for (c=0; c < TAM; c++){
    str[c] = letra;
    printf("%c ", str[c]);
 }

}

int main(){

char str[TAM], letra;

printf("Digite uma letra:");
scanf("%c", &letra);

preencher(str,letra);
return 0;

}
    
26.07.2018 / 05:37