LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2003, 03:57 PM   #1
Jo_Nak
LQ Newbie
 
Registered: Jun 2003
Posts: 20

Rep: Reputation: 0
simple problem with pointers


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:

memset( vid0, 0, sizeof(video_t) );
vid0->fd = open( "dev/video0", O_RDONLY );
if (vid0->fd == -1) return;

It works when i don't use a structure and use directly the things in it. But I absolutly need a structure.

Is this the way to initialise the pointer and to access it?

thx a lot

Jo_Nak
 
Old 06-17-2003, 04:28 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 06-17-2003, 04:46 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple c++ question (pointers) true_atlantis Programming 1 02-05-2004 06:34 PM
Yet another pointers problem in C chris.hicks Programming 3 11-10-2003 08:35 AM
Newbie problem with pointers in C. chris.hicks Programming 2 11-08-2003 02:00 PM
Simple problem with .... FredrikN Linux - Networking 1 10-27-2002 05:57 AM
Need some pointers in this problem (inside...) Vlad_M Linux - General 8 09-04-2002 03:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration