Allocating memory to an array and changing a value in

I have written very simple code, that I used to feel confident in but it isn't working. I don't know if I am doing anything wrong or perhaps it is something wrong with my vscode. I have allocated memory for an array of integers and chars, and I then use scanf to read values that I then want to save in said arrays. But it appears it is saving the values as garbage value when I use printfs to debug.

For context: tamanhos = sizes, which are supposed to be either p, P, g or G, and quantidades are supposed to be integers.

#include <stdio.h>
#include <stdlib.h>
#define professores 7
int main () {
    int quantidade, total, holder;
    char tamanho;

    total = 0;

    int * quantidades = (int*)malloc(professores*sizeof(int));
    char * tamanhos = (char*)malloc(professores*sizeof(char));

    for (int i = 0; i < 7; i++){
        scanf(" %d", &quantidade);
        scanf(" %c", &tamanho);
        quantidades[i] = quantidade;
        tamanhos[i] = tamanho;
    }

    printf("%d", total);

    for (int i = 0; i < 7; i++ ){
        if (tamanhos[i] == "P" || tamanhos[i] == "p") {
            holder = quantidades[i];
            total = total + quantidades[i]*10;
        }
        else if (tamanhos[i] == "G" || tamanhos[i] == "g") {
            total = total + quantidades[i]*16;
        }
    }
    printf("%d\n", total);
    int xprof = total / 7;
    printf("%d", xprof);

    free(quantidades);
    free(tamanhos);

    return 0;
}

Upvotes: 1

Views: 295

Answers (3)

Yun
Yun

Reputation: 3812

Main problem:

  • tamanhos[i] == "P" compares a char to "pointer to a string literal". You probably meant to use single quotes, i.e. tamanhos[i] == 'P'.

Minor points of improvement:

  • int main() is, generally, not a valid signature for this function, use int main(void) when not using the parameters.
  • The magic number 7 appears several times. Perhaps these should be replaced by the macro professores? The latter is commonly written in all caps to distinguish it from a regular variable.
  • A value is stored in holder, but this is never read.
  • Try to declare and initialize variables in one statement (e.g. int n = 5), unless you are using an old version of C which does not allow this.
  • Try to make the scope of the variables as small as possible. Simply put, declare them directly before they are used.
  • The function scanf can be tricky to use w.r.t. avoiding buffer overflows. It is best replaced by a combination of fgets followed by sscanf (or parsing the result manually).
  • You can write values directly to an array, e.g. scanf(" %d", &quantidades[i]);.

Additional credits: Oka and Andrew Henle.

Upvotes: 1

Jarvis__-_-__
Jarvis__-_-__

Reputation: 242

In your code here...

scanf(" %d", &quantidade);
scanf(" %c", &tamanho);

don't use spaces in scanf(" %d, &quantidade);

and also you can insert values in the array directly like...

scanf("%d", &quantidades[i]);
scanf("%c", &tamanhos[i]);

If I am not wrong then your compiler must be giving you a warning here...

if (tamanhos[i] == "P" || tamanhos[i] == "p") {

and here..

else if (tamanhos[i] == "G" || tamanhos[i] == "g") {

You are comparing a single character here only so use only single colon...

Upvotes: 2

user15375057
user15375057

Reputation:

In C and in C++ single quotes identify a single character, while double quotes create a string literal. You are compairing character so use single quotes instesd of double.

#include <stdio.h>
#include <stdlib.h>
#define professores 7
int main () {
    int quantidade, total = 0, holder;
    char tamanho;    
    int * quantidades = (int*)malloc(professores*sizeof(int));
    char * tamanhos = (char*)malloc(professores*sizeof(char));

    for (int i = 0; i < 7; i++){
        scanf(" %d", &quantidade);
        scanf(" %c", &tamanho);
        quantidades[i] = quantidade;
        tamanhos[i] = tamanho;
    }
    printf("%d\n", total);
    for (int i = 0; i < 7; i++ ){
        if (tamanhos[i] == 'P' || tamanhos[i] == 'p') {
            holder = quantidades[i];
            total = total + quantidades[i]*10;
        }
        else if (tamanhos[i] == 'G' || tamanhos[i] == 'g') 
            total = total + quantidades[i]*16;
    }
    printf("%d\n", total);
    int xprof = total / 7;
    printf("%d", xprof);

    free(quantidades);
    free(tamanhos);
    return 0;
}

Upvotes: 2

Related Questions