Variable declared in main is not accessed inside another function

Asked

Viewed 126 times

1

When I run the code below, it gives the following error:

'daluno' was not declared in this Scope

Code:

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

struct Aluno{

    int matricula;
    float p1;
    float p2;
    float p3;
    float t1;
    float t2;
};

void aloca(int N){
    int *pAluno = &daluno;
    pAluno = (int*)malloc(sizeof(daluno)*N);

    if (pAluno==NULL){
        printf("Memoria INSUFICIENTE");
        exit(0);
    }
    int i;
    for(i=0; i<N; i++) {
        printf(" Digite a matricula do aluno %d: ",i);
        scanf("%d", &daluno[i].matricula);
        printf(" Digite a nota da prova 1 do aluno %d: ",i);
        scanf("%d%*c", &daluno[i].p1);
        printf("\n Digite a nota da prova 2 do aluno %d: ",i);
        scanf("%f%*c", &daluno[i].p2);
        printf("\n Digite a nota da prova 3 do aluno %d: ",i);
        scanf("%f%*c", &daluno[i].p3);
        printf("\n Digite a nota do trabalho 1 do aluno %d: ",i);
        scanf("%f%*c", &daluno[i].t1);
        printf("\n Digite a nota do trabalho 2 do aluno %d: ",i);
        scanf("%f%*c", &daluno[i].t2);
    }

    for(i=0; i<N; i++) {
        printf("matricula: %d prova 1: %f prova 2: %f prova 3: %f trabalho 1: %f trabalho 2: %f\n\n", daluno[i].matricula, daluno[i].p1, daluno[i].p2, daluno[i].p3, daluno[i].t1, daluno[i].t2);
    }
    free(pAluno);
}

int main (){

    struct Aluno daluno;
    int N;

    printf("Digite a quantidade de N alunos: ");
    scanf("%d",&N);
    aloca (N);
return 0;
}
  • 3

    Yes, because it is only visible in the main function

  • So for me to allocate a vector dynamically I have to create a function?

  • 1

    It is declared in main, so can only be accessed in main.

  • You have to put it out of any function, so it becomes a global variable

  • As you created the variable 'daluno' in the main method, only the same has access to it. To access on a global level, you must initialize the variable outside the main method.

  • The main is a function, and any variable declared within a function has the scope limited to function, if you want this variable to be accessed anywhere, you have to declare it globally, in case outside any function.

  • I don’t know if it fits, but you could pass the reference (pointer) of this variable by parameter to be accessed by the function, and thus you would change the variable without declaring it globally. Ps: if I’m wrong I withdraw the comment....

Show 2 more comments

3 answers

2

This example is similar to your problem, where I declare the variable "number" within the function "main", and then call the function "showNumer", but the variable "number" is not in the scope of "showNumer", number is only accessible for the function "main".

#include <stdio.h>

void mostrarNumero(){
    printf("retultado: %d", numero);
}

int main()
{
    int numero = 1;
    mostrarNumero();
    return 0;
}

However if we declare the variable "number" outside of any function, it will be declared globally, being of global scope, any function will have access to it, as you can see in the example below.

#include <stdio.h>

int numero = 1;

void mostrarNumero(){
    printf("retultado: %d", numero);
}

int main()
{
    mostrarNumero();
    return 0;
}
  • [Error] no match for 'Operator[]' (operand types are 'Student' and 'int')... After making these changes to that error...

  • This is no longer the variable scope error, I believe this error is because you are trying to make a cast from "Student" to "int".

  • 1

    @Brunogomes I think it’s pq pAluno shall be a pointer to struct Aluno, nay int. In fact, within aloca you give malloc and free in pAluno, and you’re just using daluno to obtain the sizeof, but actually you don’t even need this variable, you can remove it and use sizeof(struct Aluno) direct: https://ideone.com/z2OZmq (it’s been a while since I’ve programmed decently in C, so my code may not be the ideal way, but as I recall, that’s the idea)

  • Ty, it worked out!! '-'

  • Please rate the response and mark as answered ;)

0

You can solve this problem by declaring the variable as global, or passing it as a function parameter aloca()

Altering

void aloca(int n, Aluno aluno)
{
 //codigo aqui
}```

0

As other users have mentioned, you should declare the variable globally in this way

#include <stdio.h> 
int numero = 1; 
void mostrarNumero(){
printf("retultado: %d", numero);
}
int main() {
mostrarNumero();
return 0; }

So you can access it from any function

Or you can pass this variable to the function in this way:

#include <stdio.h> 
void mostrarNumero(int numero2){
printf("retultado: %d", numero2);
}
int main() {
int numero = 1;
mostrarNumero(numero);
return 0; }

Browser other questions tagged

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