ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This seems like a simple problem, but I can't figure it out. In the .c file, i declared a global structure and global pointers to the structure this way:
typedef struct{
int fd;
...
} video_t;
video_t *vid0, *vid1, *vid2, *vid3;
then, i want to initialise it in my first function. This is what i did:
You're initializing the memory the pointers are indicating, but you aren't initializing the pointers themselves. You need to assign a value to the pointer before dereferencing it. Like this:
Code:
vid0 = (video_t *)malloc(sizeof(video_t));
At least, that's how I'm comfortable doing it.
THEN, you can initialize the memory it points at with memset.
You declared pointers to point to instances op the struct, but you have not made instances to point to...
Code:
#include <stdio.h>
#include <string.h>
typedef struct{
int fd;
} video_t;
video_t *vid0, *vid1, *vid2, *vid3;
/* make 4 instances of the struct */
video_t v0, v1, v2, v3;
int main()
{
/* make the pointers point to the instances */
vid0 = &v0;
vid1 = &v1;
vid2 = &v2;
vid3 = &v3;
/* you can now do a memset on them: (only one here: vid0) */
memset( vid0, 0, sizeof(video_t) );
return 0;
}
Instead of declaring normal variable of type video_t and then assign the pointers to them, you can also create them dynamically with malloc() which returns the pointer you will want to store in the pointer variables:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct{
int fd;
} video_t;
video_t *vid0, *vid1, *vid2, *vid3;
int main()
{
/* make an instance (only one here for the */
/* example) on the heap with malloc() */
/* which returns a pointer to the struct */
/* floating somewhere in memory: */
vid0 = (video_t *) malloc(sizeof(video_t));
memset( vid0, 0, sizeof(video_t) );
/* You need to free the memory allocated */
/* by malloc() explicitly. */
free(vid0);
return 0;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.