void * problem for spreadsheet

This is a discussion on void * problem for spreadsheet within the C Programming forums, part of the General Programming Boards category; Well hello everybody.i planned to develop simple spreadsheet for my homeworks,and then i decided to use List instead of matrix ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    void * problem for spreadsheet

    Well hello everybody.i planned to develop simple spreadsheet for my homeworks,and then
    i decided to use List instead of matrix to represent the spreadsheet cell.

    well, code below is type declaration for my list in file matriks.h
    Code:
    /********** DEKLARASI matriks  **********/
    /* File : matriks.h        */
    /* deklarasi list untuk representasi sel pada matriks */
    #ifndef matriks_H
    #define matriks_H
    #include "boolean.h"
    #include <stdlib.h>
    
    typedef struct
    {
    	void *value;
    	int tipe;
    }infotype;
    
    typedef struct tElmtCell *address;
    
    typedef struct tElmtCell 
    {
    	infotype *Info;
    	int row;
    	int column;
    	address Next;
    }ElmtCell;
    
    
    typedef struct 
    {
    	address FirstCell;
    }Matriks;
    
    /*** Selector Notation***/
    
    #define Nil       NULL
    #define First(M)  (M).FirstCell
    #define NextCell(C)  (C)->Next
    #define Info(C)  (C)->Info->value
    #define Tipe(C)  (C)->Info->tipe
    #define Row(C) (C)->row
    #define Column(C) (C)->column
    
    
    /*** Prototype Primitif ***/
    
    void CreateMatriks(Matriks *M);
    boolean IsEmptyMatriks(Matriks M);
    address Allocate(int tipe,void *value,int row,int column);
    The list node contains structure named infotype which hold the value of cell and the Value type.1 for integer type,2 string for type,and 3 for storing formula.So i use void * in order to be able to store any kind of data type (integer,string,etc)

    this is file matriks.c contains function body from matriks.h

    Code:
    void CreateMatriks(Matriks *M)
    {
    	First(*M) = Nil;
    }
    
    boolean IsEmptyMatriks(Matriks M)
    {
    	return (First(M) == Nil);
    }
    
    
    address Alllocate(int tipe,void *value,int row,int column)
    {
    	
    	address P;
    	printf("setelah alokasi \n");
    	P=(address) malloc(sizeof(ElmtCell));
    	printf("alokasi \n");
    	if (P!=Nil)
    	{
    		Row(P) = rows;  
    		Column(P) = column;
    		Tipe(P)  = tipe;
    		Info(P)  = isi;
    		NextCell(P) = Nil;		
    	}
    	
    	return (P);
    }
    well the problem is when i want to allocate some memory space for new list node,i use
    Allocate function (umm, i made this function,you can see the function body in code above).The strange part is i can only use the Allocate function just once,you can try this code below to test the allocate function

    Code:
    #include "matriks.h"
    
    int main (void)
    {
    	Matriks M1;
    	CreateMatriks(&M1);
    	address P = Allocate(1,(void *)1,1,1);
    	address Q = Allocate(1,(void *)2,2,1);
    	/*
    	printf("it works \n");
    	//InsertElement(&M1,1,1,1,1);
    	printf("info cell is : %d \n",Info(First(M1)));
    	//InsertElement(&M1,2,"rama adhitia",2,23);
    	printf("hehe\n");*/
    	scanf("%d");
    	return 0;
    }
    So when i allocate address P the allocate function works nicely,but when i allocate address Q,it didnt work.I guess this problem occured because i use void * to store value in infotype structure.

    Please help me guys!!

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How about not casting malloc, which you shouldn't be doing in the first place ?

    If that doesn't work, what errors are you getting ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    You are using
    (C)->Info->value
    while the Info pointer is dangling - it point nowhere... (actually it can point anywhere)
    so when you write
    Info(P) = isi;
    your program should crash in most cases.
    You should allocate memory for the Info struct also, before using it.

    Or make it a struct member - not a pointer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    4

    thank you

    hi,thank you for the solution vart,my code is working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  3. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21