i can't declare a pointer as a global variable [duplicate]












0
















This question already has an answer here:




  • use malloc out of main

    3 answers




I'm trying to run this program that manage login but I have a problem with declaring this pointer as a global variable , the error that it shows me is
"initializer element is not constant"



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
}USER;


USER *u =(USER *)malloc(sizeof(USER)*20);

int nbr_usr=0;
void adduser() {};
......









share|improve this question















marked as duplicate by usr c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    It is not the declaration by itself but the initialization that you are providing. In C, code outside functions cannot be executed, you'd have to put that malloc inside a function. (And don''t cast the return of malloc)

    – Jens Gustedt
    Nov 23 '18 at 10:25













  • You need to have a main().

    – Sourav Ghosh
    Nov 23 '18 at 10:25











  • Also please, go through the help pages to learn how to ask questions. We usually need more information about what you are doing. In particular a whole minimal code example.

    – Jens Gustedt
    Nov 23 '18 at 10:28
















0
















This question already has an answer here:




  • use malloc out of main

    3 answers




I'm trying to run this program that manage login but I have a problem with declaring this pointer as a global variable , the error that it shows me is
"initializer element is not constant"



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
}USER;


USER *u =(USER *)malloc(sizeof(USER)*20);

int nbr_usr=0;
void adduser() {};
......









share|improve this question















marked as duplicate by usr c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 4





    It is not the declaration by itself but the initialization that you are providing. In C, code outside functions cannot be executed, you'd have to put that malloc inside a function. (And don''t cast the return of malloc)

    – Jens Gustedt
    Nov 23 '18 at 10:25













  • You need to have a main().

    – Sourav Ghosh
    Nov 23 '18 at 10:25











  • Also please, go through the help pages to learn how to ask questions. We usually need more information about what you are doing. In particular a whole minimal code example.

    – Jens Gustedt
    Nov 23 '18 at 10:28














0












0








0









This question already has an answer here:




  • use malloc out of main

    3 answers




I'm trying to run this program that manage login but I have a problem with declaring this pointer as a global variable , the error that it shows me is
"initializer element is not constant"



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
}USER;


USER *u =(USER *)malloc(sizeof(USER)*20);

int nbr_usr=0;
void adduser() {};
......









share|improve this question

















This question already has an answer here:




  • use malloc out of main

    3 answers




I'm trying to run this program that manage login but I have a problem with declaring this pointer as a global variable , the error that it shows me is
"initializer element is not constant"



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
}USER;


USER *u =(USER *)malloc(sizeof(USER)*20);

int nbr_usr=0;
void adduser() {};
......




This question already has an answer here:




  • use malloc out of main

    3 answers








c pointers






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 10:30









Jabberwocky

26.7k93770




26.7k93770










asked Nov 23 '18 at 10:23









Deva KhDeva Kh

11




11




marked as duplicate by usr c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by usr c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:34


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 4





    It is not the declaration by itself but the initialization that you are providing. In C, code outside functions cannot be executed, you'd have to put that malloc inside a function. (And don''t cast the return of malloc)

    – Jens Gustedt
    Nov 23 '18 at 10:25













  • You need to have a main().

    – Sourav Ghosh
    Nov 23 '18 at 10:25











  • Also please, go through the help pages to learn how to ask questions. We usually need more information about what you are doing. In particular a whole minimal code example.

    – Jens Gustedt
    Nov 23 '18 at 10:28














  • 4





    It is not the declaration by itself but the initialization that you are providing. In C, code outside functions cannot be executed, you'd have to put that malloc inside a function. (And don''t cast the return of malloc)

    – Jens Gustedt
    Nov 23 '18 at 10:25













  • You need to have a main().

    – Sourav Ghosh
    Nov 23 '18 at 10:25











  • Also please, go through the help pages to learn how to ask questions. We usually need more information about what you are doing. In particular a whole minimal code example.

    – Jens Gustedt
    Nov 23 '18 at 10:28








4




4





It is not the declaration by itself but the initialization that you are providing. In C, code outside functions cannot be executed, you'd have to put that malloc inside a function. (And don''t cast the return of malloc)

– Jens Gustedt
Nov 23 '18 at 10:25







It is not the declaration by itself but the initialization that you are providing. In C, code outside functions cannot be executed, you'd have to put that malloc inside a function. (And don''t cast the return of malloc)

– Jens Gustedt
Nov 23 '18 at 10:25















You need to have a main().

– Sourav Ghosh
Nov 23 '18 at 10:25





You need to have a main().

– Sourav Ghosh
Nov 23 '18 at 10:25













Also please, go through the help pages to learn how to ask questions. We usually need more information about what you are doing. In particular a whole minimal code example.

– Jens Gustedt
Nov 23 '18 at 10:28





Also please, go through the help pages to learn how to ask questions. We usually need more information about what you are doing. In particular a whole minimal code example.

– Jens Gustedt
Nov 23 '18 at 10:28












2 Answers
2






active

oldest

votes


















2














In C you cannot call functions during initialisation of global variables.



You need to do the initialisation for example in main:



...
USER *u;
...
int main()
{
u = malloc(sizeof(USER)*20);
...
}


BTW:




  1. you don't need to cast the result of malloc.

  2. the variable name u is not a good idea. Give it a meaningful name, e.g. users.


Not directly related to your question, but be aware that the 20 in the struct declaration below is not at all related to the 20 in malloc(sizeof(USER)*20).



typedef struct {
char nom_utilisateur[20];
char mot_de_passe[20];
char nom[20];
char prenom[20];
} USER;


in this struct declaration the 20 means that each user's password, nom, prenom etc. can have a length of 19 at most, 19 because strings are NUL terminated.



The 20 in malloc(sizeof(USER)*20) means that you can have at most 20 users.






share|improve this answer

































    1














    The problem is not the declaration.
    The problem is that you are initializating the pointer with a runtime information.
    Think about malloc as something that can only be executed when the program is actually running. Such stuff must be placed in a function.



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct {
    char nom_utilisateur[20];
    char mot_de_passe[20];
    char nom[20];
    char prenom[20];
    }USER;


    USER *u;

    int nbr_usr=0;
    void adduser() {};
    void initialize(){
    u = (USER *)malloc(sizeof(USER)*20);
    //do all the other initializations here
    }
    int main(){
    initialize();
    return 0;
    }





    share|improve this answer
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      In C you cannot call functions during initialisation of global variables.



      You need to do the initialisation for example in main:



      ...
      USER *u;
      ...
      int main()
      {
      u = malloc(sizeof(USER)*20);
      ...
      }


      BTW:




      1. you don't need to cast the result of malloc.

      2. the variable name u is not a good idea. Give it a meaningful name, e.g. users.


      Not directly related to your question, but be aware that the 20 in the struct declaration below is not at all related to the 20 in malloc(sizeof(USER)*20).



      typedef struct {
      char nom_utilisateur[20];
      char mot_de_passe[20];
      char nom[20];
      char prenom[20];
      } USER;


      in this struct declaration the 20 means that each user's password, nom, prenom etc. can have a length of 19 at most, 19 because strings are NUL terminated.



      The 20 in malloc(sizeof(USER)*20) means that you can have at most 20 users.






      share|improve this answer






























        2














        In C you cannot call functions during initialisation of global variables.



        You need to do the initialisation for example in main:



        ...
        USER *u;
        ...
        int main()
        {
        u = malloc(sizeof(USER)*20);
        ...
        }


        BTW:




        1. you don't need to cast the result of malloc.

        2. the variable name u is not a good idea. Give it a meaningful name, e.g. users.


        Not directly related to your question, but be aware that the 20 in the struct declaration below is not at all related to the 20 in malloc(sizeof(USER)*20).



        typedef struct {
        char nom_utilisateur[20];
        char mot_de_passe[20];
        char nom[20];
        char prenom[20];
        } USER;


        in this struct declaration the 20 means that each user's password, nom, prenom etc. can have a length of 19 at most, 19 because strings are NUL terminated.



        The 20 in malloc(sizeof(USER)*20) means that you can have at most 20 users.






        share|improve this answer




























          2












          2








          2







          In C you cannot call functions during initialisation of global variables.



          You need to do the initialisation for example in main:



          ...
          USER *u;
          ...
          int main()
          {
          u = malloc(sizeof(USER)*20);
          ...
          }


          BTW:




          1. you don't need to cast the result of malloc.

          2. the variable name u is not a good idea. Give it a meaningful name, e.g. users.


          Not directly related to your question, but be aware that the 20 in the struct declaration below is not at all related to the 20 in malloc(sizeof(USER)*20).



          typedef struct {
          char nom_utilisateur[20];
          char mot_de_passe[20];
          char nom[20];
          char prenom[20];
          } USER;


          in this struct declaration the 20 means that each user's password, nom, prenom etc. can have a length of 19 at most, 19 because strings are NUL terminated.



          The 20 in malloc(sizeof(USER)*20) means that you can have at most 20 users.






          share|improve this answer















          In C you cannot call functions during initialisation of global variables.



          You need to do the initialisation for example in main:



          ...
          USER *u;
          ...
          int main()
          {
          u = malloc(sizeof(USER)*20);
          ...
          }


          BTW:




          1. you don't need to cast the result of malloc.

          2. the variable name u is not a good idea. Give it a meaningful name, e.g. users.


          Not directly related to your question, but be aware that the 20 in the struct declaration below is not at all related to the 20 in malloc(sizeof(USER)*20).



          typedef struct {
          char nom_utilisateur[20];
          char mot_de_passe[20];
          char nom[20];
          char prenom[20];
          } USER;


          in this struct declaration the 20 means that each user's password, nom, prenom etc. can have a length of 19 at most, 19 because strings are NUL terminated.



          The 20 in malloc(sizeof(USER)*20) means that you can have at most 20 users.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 10:44

























          answered Nov 23 '18 at 10:33









          JabberwockyJabberwocky

          26.7k93770




          26.7k93770

























              1














              The problem is not the declaration.
              The problem is that you are initializating the pointer with a runtime information.
              Think about malloc as something that can only be executed when the program is actually running. Such stuff must be placed in a function.



              #include <stdio.h>
              #include <stdlib.h>
              #include <string.h>
              typedef struct {
              char nom_utilisateur[20];
              char mot_de_passe[20];
              char nom[20];
              char prenom[20];
              }USER;


              USER *u;

              int nbr_usr=0;
              void adduser() {};
              void initialize(){
              u = (USER *)malloc(sizeof(USER)*20);
              //do all the other initializations here
              }
              int main(){
              initialize();
              return 0;
              }





              share|improve this answer






























                1














                The problem is not the declaration.
                The problem is that you are initializating the pointer with a runtime information.
                Think about malloc as something that can only be executed when the program is actually running. Such stuff must be placed in a function.



                #include <stdio.h>
                #include <stdlib.h>
                #include <string.h>
                typedef struct {
                char nom_utilisateur[20];
                char mot_de_passe[20];
                char nom[20];
                char prenom[20];
                }USER;


                USER *u;

                int nbr_usr=0;
                void adduser() {};
                void initialize(){
                u = (USER *)malloc(sizeof(USER)*20);
                //do all the other initializations here
                }
                int main(){
                initialize();
                return 0;
                }





                share|improve this answer




























                  1












                  1








                  1







                  The problem is not the declaration.
                  The problem is that you are initializating the pointer with a runtime information.
                  Think about malloc as something that can only be executed when the program is actually running. Such stuff must be placed in a function.



                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <string.h>
                  typedef struct {
                  char nom_utilisateur[20];
                  char mot_de_passe[20];
                  char nom[20];
                  char prenom[20];
                  }USER;


                  USER *u;

                  int nbr_usr=0;
                  void adduser() {};
                  void initialize(){
                  u = (USER *)malloc(sizeof(USER)*20);
                  //do all the other initializations here
                  }
                  int main(){
                  initialize();
                  return 0;
                  }





                  share|improve this answer















                  The problem is not the declaration.
                  The problem is that you are initializating the pointer with a runtime information.
                  Think about malloc as something that can only be executed when the program is actually running. Such stuff must be placed in a function.



                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <string.h>
                  typedef struct {
                  char nom_utilisateur[20];
                  char mot_de_passe[20];
                  char nom[20];
                  char prenom[20];
                  }USER;


                  USER *u;

                  int nbr_usr=0;
                  void adduser() {};
                  void initialize(){
                  u = (USER *)malloc(sizeof(USER)*20);
                  //do all the other initializations here
                  }
                  int main(){
                  initialize();
                  return 0;
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 10:40

























                  answered Nov 23 '18 at 10:35









                  Davide SpataroDavide Spataro

                  4,32611128




                  4,32611128















                      Popular posts from this blog

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'