Passing pointers to struct

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Passing pointers to struct
# 1  
Old 01-08-2011
Passing pointers to struct

Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function with a pointer to my struct and write only creates the file and doesn't write anything. I'm pretty sure the problem i have has to do with the pointers because when i execute
Code:
printf("%i",sizeof(*pointer_to_the_struct)

in bwrite function it says 1 and should be 48

Here are the important parts of the code:

Code:
struct superbloque{
    unsigned int firstmbits;
    unsigned int lastmbits
    ...
    unsigned int numbloques;
}

int initSB (unsigned int nbloq){
    struct superbloque SB;
    SB.firstmbits=0;
    SB.lastmbits=TamMB(nbloq)-1;
    ...
    SB.numbloques=nbloq;
    bwrite(0,&SB)
}

int bwrite(unsigned int bloque, void *buf){
    lseek(mifd, bloque*tbloque, SEEK_SET);
    return write(mifd, buf, sizeof(*buf);
}

I know there's a lot of information about this but i've been reading for two days and I can't figure out how to solve the problem.
Thank you very much for your help


Ricardo Galli
Sistemas operativos
Universidad de las Islas Baleares (Palma de Mallorca, Spain)

Last edited by pludi; 01-08-2011 at 02:52 PM..
# 2  
Old 01-08-2011
The size of a void is 1, so only 1 byte is getting written. You need to provide the size of the structure, either:
Code:
int bwrite(unsigned int bloque, void *buf, size_t siz){
    lseek(mifd, bloque*tbloque, SEEK_SET);
    return write(mifd, buf, siz);
}

or
Code:
 int bwrite(unsigned int bloque, struct superbloque  *buf){
     lseek(mifd, bloque*tbloque, SEEK_SET);
     return write(mifd, buf, sizeof(*buf));
 }


Last edited by m.d.ludwig; 01-08-2011 at 09:59 PM.. Reason: superfluous word
# 3  
Old 01-24-2011
Quote:
Originally Posted by ignatius3
Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function with a pointer to my struct and write only creates the file and doesn't write anything. I'm pretty sure the problem i have has to do with the pointers because when i execute
Code:
printf("%i",sizeof(*pointer_to_the_struct)

in bwrite function it says 1 and should be 48

Here are the important parts of the code:

Code:
struct superbloque{
    unsigned int firstmbits;
    unsigned int lastmbits
    ...
    unsigned int numbloques;
}

int initSB (unsigned int nbloq){
    struct superbloque SB;
    SB.firstmbits=0;
    SB.lastmbits=TamMB(nbloq)-1;
    ...
    SB.numbloques=nbloq;
    bwrite(0,&SB)
}

int bwrite(unsigned int bloque, void *buf){
    lseek(mifd, bloque*tbloque, SEEK_SET);
    return write(mifd, buf, sizeof(*buf);
}

I know there's a lot of information about this but i've been reading for two days and I can't figure out how to solve the problem.
Thank you very much for your help


Ricardo Galli
Sistemas operativos
Universidad de las Islas Baleares (Palma de Mallorca, Spain)
if pointer_to_the_struct is a pointer to the structure, then *pointer_to_the_struct is the first byte in the structure. so you a integer of 1
you should try
printf("%i",sizeof(struct xxx))
and this will get you the real size of the structure, and that's what you want
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Storing C++-struct in file - problem when adding new item in struct

Hi, I have received an application that stores some properties in a file. The existing struct looks like this: struct TData { UINT uSizeIncludingStrings; // copy of Telnet data struct UINT uSize; // basic properties: TCHAR szHost; //defined in Sshconfig UINT iPortNr; TCHAR... (2 Replies)
Discussion started by: Powerponken
2 Replies

2. Programming

Passing Pointers by reference in C++ Problem

Hello All, I am having this issue...where I am actually having hard time understanding the problem: The code is as follows: #include<iostream.h> void fxn(char*** var) { int i =4; *var = (char**)malloc(i*sizeof(char*)); for(int j =0; j<4; j++) { *var = "name"; cout<<*var;... (6 Replies)
Discussion started by: mind@work
6 Replies

3. Programming

Using pointers to struct members as args to functions

In a well-known book on the C language, there is an example of an efficient method for using a struct member as an argument to a function. (I'm a C noob, but I believe the correct terminology might be: use call-by-reference instead of call-by-value.) The function is printf. Anyway, here's a... (5 Replies)
Discussion started by: uiop44
5 Replies

4. Programming

Passing an instance of struct to functions in other src files

I am trying to work out the best syntax for a relatively simple operation. The goal is to declare an instance of a struct and pass it around to be populated and have the data manipulated. There is an extra wrinkle in that the functions are in different src files. The main is simple, #include... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

5. UNIX for Dummies Questions & Answers

How to access a struct within a struct?

Can someone tell me how to do this? Just a thought that entered my mind when learning about structs. First thought was: struct one { struct two; } struct two { three; } one->two->three would this be how you would access "three"? (1 Reply)
Discussion started by: unbelievable21
1 Replies

6. UNIX for Dummies Questions & Answers

Passing struct through unix pipe -solved

EDIT: Nevermind, called a friend who is good at this stuff and he figured it out :D Hi all, So I'm trying to teach myself to write programs for unix in c. I am currently creating a program, and I need to pass a struct through a pipe, but I can't figure out how. The struct I want to pass... (0 Replies)
Discussion started by: twnsfn34
0 Replies

7. Homework & Coursework Questions

C++ struct pointers & functions

Hi All, My latest assignment (practice not coursework!) is to write prototype interactive exam/test console application. I've used structs to store the question information (not sure if this was the best way to do it?) and I have the following code that outputs each question and it's possible... (0 Replies)
Discussion started by: pondlife
0 Replies

8. UNIX for Advanced & Expert Users

Passing socket struct between kernel threads - module programming

I write kernel module with kernel threads using linux/kthread.h on 2.6.* kernel I tried to passing data between two kernel threads with data argument of kthread_run( fun, data , NAME ); but this is not work I dont know why. I tried many possibility and nothing works. So I thought that... (0 Replies)
Discussion started by: marcintom
0 Replies

9. Programming

Pointer to a struct (with pointers) *** glibc detected *** double free

I am using a structure defined as follows struct gene_square { double *x; double *y; };I have class, with a member function which is a pointer of this type: gene_square* m_Genes;I am allocating memory in the constructors like this: m_Genes = new gene_square; for (ii=0;... (1 Reply)
Discussion started by: jatoo
1 Replies

10. Programming

Need a simple example of passing FILE pointers

Dear friends, can anybody pls tell me how to pass FILE pointer in c. I am so confused .. :confused: suppose I ve two function 1. file_open() 2. read_line() I want to call these function from main() function and in file_open() function it will open that file and in read_line()... (5 Replies)
Discussion started by: user_prady
5 Replies
Login or Register to Ask a Question

Featured Tech Videos