Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

270
Visualizações
where to declare structures, inside main() or outside main()?

Case 1: structure declared outside main() working fine

#include<stdio.h>
#include<conio.h>
struct prod
{
    int price,usold;
};
int main()
{
    struct prod *p,a;

    int billamt(struct prod *);
    int bill;
    printf("enter the values \n");
    scanf("%d%d",&p->price,&p->usold);
    bill=billamt(p);
    printf("bill=%d",bill);
    getch();
}
int billamt(struct prod *i)
{
    int b;
    b=(i->price*i->usold);
    return b;
}

Case 2: declared inside main() giving error

[Error] type 'main()::prod' with no linkage used to declare function 'int billamt(main()::prod*)' with linkage [-fpermissive]*

#include<stdio.h>
#include<conio.h>
int main()
{
    struct prod
{
    int price,usold;
};


    struct prod *p,a;

    int billamt(struct prod *);
    int bill;
    printf("enter the values \n");
    scanf("%d%d",&p->price,&p->usold);
    bill=billamt(p);
    printf("bill=%d",bill);
    getch();
}
int billamt(struct prod *i)
{
    int b;
    b=(i->price*i->usold);
    return b;
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

where to declare structures, inside main() or outside main()?

  1. First thing, I think you meant "define", not "declare".

  2. Second, there is no rule as such, You can define wherever you want. It is all about the scope of the definition.

    • If you define the structure inside main(), the scope is limited to main() only. Any other function cannot see that definition and hence, cannot make use of that structure definition.

    • If you define the structure in a global scope, (i.e., outside main() or any other function, for that matter), that definition is available globally and all the functions can see and make use of the structure definition.

over 4 years ago · Santiago Trujillo Relatório

0

It have to do about scoping, when you define the structure inside the main function then it's only defined in the scope of the main function, so the billamt function can't know about it.

over 4 years ago · Santiago Trujillo Relatório

0

Structure are a like array only the main difference in Array can hold only same type of values but a structure can have different types of values so if you need to implement structure globally(by globally i mean it can be used in any other function too) define it outside the main and if you want to use your structure only in the main function define it inside it. Happy Coding :-)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda