Thread: Pointer to pointer, dynamic memory, help pls

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    69

    Pointer to pointer, dynamic memory, help pls

    Hello, I'm trying to write a program which makes a dynamic array of all different words, so if a word is already entered before, it doesn't add the word.

    This is my code:
    Code:
    /* AUTH: Kevin Strijbos
       DATE: 07/02/2012
       DESC: excercise 14.5.1 */
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    #define MAX_WORDS 10
    
    
    int check_appearance (char* *words, char *toCheck, int index);
    
    
    int main (void)
    {
    	char* *pntr;
    	char buffer[21];
    	int counter, index;
    
    
    	pntr = NULL; // initialize pntr
    	index = 0; // keeps track of the amount of words in pntr
    
    
    	for (counter = 0; counter < MAX_WORDS ;counter++)
    	{
    		printf("Enter a word: ");
    		fgets(buffer, sizeof(buffer), stdin);
    
    
    		/* delete newline */
    		if (buffer[strlen(buffer)-1] == '\n')
    			buffer[strlen(buffer)-1] = '\0';
    
    
    		if (check_appearance(pntr, buffer, index) == 0)
    		{
    			pntr = (char* *)(realloc(pntr, sizeof(char *) * (index+1)));
    			*(pntr+index) = (char *)(malloc(sizeof(buffer)+1)); // +1 for the \0
    			strcpy((*(pntr)+index), &buffer[0]);
    			index++;
    		}
    	}
    
    
    	/* print list */
    	for (counter = 0; counter < index ;counter++)
    	{
    		printf("WORD %d: %s\n", *(*(pntr+counter)));
    		free(*(pntr)+counter);
    	}
    	
    	free(pntr);
    
    
    	return 0;
    }
    
    
    /* checks if a word is already entered */
    int check_appearance (char* *words, char *toCheck, int index)
    {
    	int counter, found;
    
    
    	found = 0;
    
    
    	for (counter = 0; found == 0 && counter <= index ;counter++) // index: how many words there are in the list - 1 (counter is passed)
    	{
    		if (strcmp(*(words + counter), toCheck) == 0)
    			found = 1;
    	}
    
    
    	return found;
    }
    Now, the program doesn't work. I think the problem has something to do with the strcmp and strcpy.

    It compiles and runs, but when I enter a word I get an error.
    Anyone got an idea?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    One of your printf statements is missing parameters.
    Code:
    $ gcc -W -Wall -Wextra bar.c
    bar.c: In function ‘main’:
    bar.c:52: warning: too few arguments for format
    > for (counter = 0; found == 0 && counter <= index ;counter++)
    You're running off the end of the array with <=

    You'll find it a lot easier if you replace all your *(ptr+index) with ptr[index] notation.
    Especially since you get it wrong in a place or two.
    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.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Didn't know I can use the index notation with pointers, thanks for the tip, I'll try it that way!

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    69
    Damn, still don't find it. Been stuck on this for hours now.

    Well, I'll explain my theory:
    When it finds a word that isn't in the list yet, it adds one char pointer to the memory block where pntr is pointing to.
    Then, I let that char pointer point to a memory block with the size of the entered word.

    How I compare the list and the word:
    I check for every word in the list if it's matched with the word...

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Did you fix this one pointed out by Salem:
    One of your printf statements is missing parameters.
    And have you changed it to bracket notation?
    Post your current code.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    There are so many problems in your code...

    1. there are no words in your list... and you try to compare it with input word? from where the compiler will generate a list?????
    2. <b> char buffer[21]; /</b> this one should be memset to 0 or NULL <advice>.
    3. words is pointing to no where that is why it is getting crashed..

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    There are so many problems in your code...

    1. there are no words in your list... and you try to compare it with input word? from where the compiler will generate a list?????
    2. <b> char buffer[21]; /</b> this one should be memset to 0 or NULL <advice>.
    3. words is pointing to no where that is why it is getting crashed..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  2. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  3. Deallocating Memory allocated to a pointer to a pointer
    By darklord1984 in forum C++ Programming
    Replies: 8
    Last Post: 12-31-2005, 11:09 AM
  4. Pointer/Dynamic Memory/function problem
    By Artist_of_dream in forum C++ Programming
    Replies: 17
    Last Post: 12-26-2004, 05:57 PM
  5. void pointer and dynamic memory help
    By hans in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2002, 09:46 PM