Thread: Creating data, but data nowhere

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    30

    Creating data, but data nowhere

    Hello, i got some problem, when creating data trought another .c file (taken from header) which doesnt contain any main(). So question is: what to do that it will not dropping my data somewhere. Use some static or something else?

    My program is linked from files:
    Code:
    tab.h 
    tab.c (here is #ifndef #include "serverapp.h" #endif)
    serverapp.h
    serverapp.c (here is main() and #ifndef #include "tab.h" #endif)
    so i feel that im using tab.c (tab.h) wrong when creating througth that "data" - structures

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Put the definitions of your data structures in the .h file.
    Also look at the keyword .. extern .. which lets you access data across source pages.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    Code:
    /*
     * tab.h
     *
     *  Created on: 21.11.2010
     *      Author: Martin
     */
    
    #ifndef TAB_H_
    #define TAB_H_
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    // column is item of list of names of columns
    // Column *next - pointer on next item in list
    
    typedef struct Column {
    	char name[10];
    	struct Column *next;
    	//struct Column *previous;
    } column;
    
    // item of row, items in row complete list
    typedef struct Item {
    	int value;
    	//struct Column *itemof;
    	struct Item *next;
    } item;
    
    // row is pointer on list of items
    typedef struct Row {
    	item *first_item;
    	//struct Column *itemof;
    	struct Row *next;
    } row;
    
    // tab is "pointer" on head row inculde count of columns and names of them
    typedef struct Tab {
    	char name[10];
    	int columns_count;
    	int rows_count;
    	column *first_col_name;
    	struct Tab *next;
    	row *first_row;
    	row *spec_iter_row;
    	//struct List *previous;
    } tab;
    
    void print_row(tab *which_from, row *for_print);
    
    column *create_list_of_names(char *column_names);
    
    tab *create_tab(char *name, char *column_names);
    
    int *create_row(int values[], char tab_name[]);
    
    int delete_row(tab *tab_temp,row *for_delete);
    
    void delete_all_rows(char tab_name[]);
    
    void delete_from(char tab_name[], char column_name[], int value);
    
    void delete_tab(char tab_name[]);
    
    int delete_all(char passwd[]);
    
    tab *find_tab(char tab_name[]);
    
    row *find_value(char tab_name[], char column_name[], int value, int ctr);
    
    void select_from(char tab_name[], char column_name[], int value);
    
    void print_all(char tab_name[]);
    
    //void add_item(item **head, char *name, int age);
    extern int tab_count;
    extern tab *first_tab;
    extern tab *last_tab;
    
    #endif /* TAB_H_ */

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    so if i understood right, i will use this externs there where i want to save them yes ?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Well you get past the need to "store some magic internal variable" if you arrange the interface like so.

    Code:
    tab *create_tab(char *name, char *column_names);
    void print_tab(tab *tab);
    void delete_tab(tab *tab);
    The create function returns a tab*, and ALL other interfaces receive a tab*

    Follow something like fopen() (returns a 'token' as a FILE*), and everything else (fclose, fread, fgets, etc etc) all take a FILE* as a parameter.
    What a FILE* actually is is irrelevant to the users of the service.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Holymanus View Post
    so if i understood right, i will use this externs there where i want to save them yes ?
    The extern keyword simply means "created elsewhere"...
    Code:
    //first.c
    int x; 
    
    
    // second.c
    extern int x;
    second.c has access to x from first.c Placing them in a header is just a convenience so they are available everywhere the header is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM