i can't declare a pointer as a global variable [duplicate]
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() {};
......
c pointers
marked as duplicate by usr
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.
add a comment |
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() {};
......
c pointers
marked as duplicate by usr
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 thatmalloc
inside a function. (And don''t cast the return ofmalloc
)
– Jens Gustedt
Nov 23 '18 at 10:25
You need to have amain()
.
– 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
add a comment |
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() {};
......
c pointers
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
c pointers
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
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
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 thatmalloc
inside a function. (And don''t cast the return ofmalloc
)
– Jens Gustedt
Nov 23 '18 at 10:25
You need to have amain()
.
– 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
add a comment |
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 thatmalloc
inside a function. (And don''t cast the return ofmalloc
)
– Jens Gustedt
Nov 23 '18 at 10:25
You need to have amain()
.
– 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
add a comment |
2 Answers
2
active
oldest
votes
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:
- you don't need to cast the result of
malloc
. - 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.
add a comment |
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;
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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:
- you don't need to cast the result of
malloc
. - 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.
add a comment |
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:
- you don't need to cast the result of
malloc
. - 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.
add a comment |
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:
- you don't need to cast the result of
malloc
. - 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.
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:
- you don't need to cast the result of
malloc
. - 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.
edited Nov 23 '18 at 10:44
answered Nov 23 '18 at 10:33
JabberwockyJabberwocky
26.7k93770
26.7k93770
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Nov 23 '18 at 10:40
answered Nov 23 '18 at 10:35
Davide SpataroDavide Spataro
4,32611128
4,32611128
add a comment |
add a comment |
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 ofmalloc
)– 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