2D Array and pointer. Help!

This is a discussion on 2D Array and pointer. Help! within the C Programming forums, part of the General Programming Boards category; I'm trying to read in the dictionary with a name and a number, but it doesn't work. I'm doing a ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    6

    2D Array and pointer. Help!

    I'm trying to read in the dictionary with a name and a number, but it doesn't work. I'm doing a lot of mistakes... Can somebody point out where I need to correct?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include "Dictionary.h"
    
    int count=0;
    
    
    int main(){
    
    	dic dicptrs[20];
    	char k[20], v[20];
    	int insert;
    	dicptrs[count]= CreateDictionary();
    	printf("Enter a name:");
    	scanf("%s", &k);
    	printf("Enter a number:");
    	scanf("%s", &v);
    	insert = InsertEntries(dicptrs[20],k,v);
    	printf("%s and %s\n", dicptrs->keys, dicptrs->values);
    
    }
    
    /****************CreateDictionary******************************/
    Dictionary* CreateDictionary(void){
    /* Pre:		True
     * Post:	size of dictionary = 0 && count = 0
     */
    	dic dicptr;
    	dicptr = (Dictionary*) malloc(sizeof(Dictionary));
        dicptr->keys = dicptr->values = NULL;
        return dicptr;
    
    }	
    
    /***************InsertEntries********************************/
    int InsertEntries(Dictionary* dicptr, char key[], char value[]){
    
    	strcpy(dicptr->keys,key);
    	strcpy(dicptr->values,value);
    	return 1;
    }
    And here is the header file:

    Code:
    #ifndef DICTIONARY_HDR
    #define DICTIONARY_HDR
    
    typedef struct dictionary { 
            char* keys[20]; 
            char* values[20]; 
            int count; 
    } Dictionary, *dic;
    
    
    
    Dictionary* CreateDictionary(void);
    /* Pre:		True
     * Post:	size of dictionary = 0 && count = 0
     */
    
    
    int InsertEntries(Dictionary *dic,char[], char value[]);
    /* Pre:		size dic >=0
     * Post:	Return 1 if the insertion was perfomed and 0 otherwise
     *			size dic = size dic0+1
     */
    
    #Endif

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Your problem is consistent at least. :-) You're forgetting that the thing to the left of the -> operator in just about every case is an array of pointers, not a single pointer. You have to add a subscript, like this
    Code:
    dicptr->keys[0] = dicptr->values[0] = NULL;
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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