Global arrays, file I/O and pointers

This is a discussion on Global arrays, file I/O and pointers within the C Programming forums, part of the General Programming Boards category; Hi there, I'm a programming newbie and I can't seem to get this section of code to work. I read ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Global arrays, file I/O and pointers

    Hi there, I'm a programming newbie and I can't seem to get this section of code to work. I read in the lines of a file, but immediately after the loop that creates an array of the strings in the file, all char pointers point to the last item in the list.

    Code:
    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    
    typedef char * string;
    string *dictionary;
    int num_lines;
    
    
    void *thread1(char *input_word){
    		pthread_mutex_lock(&mutex);
    		pthread_mutex_unlock(&mutex);
    }
    
    void *thread2(char *input_word){
    		pthread_mutex_lock(&mutex);
    		pthread_mutex_unlock(&mutex);
    }
    
    
    main(){	
    
    	//count number of words in dictionary file
    	FILE *input = fopen("dictionary.txt","r");
    	num_lines=0;
    	char temp[50];
    	while (fscanf(input,"%s",temp)!=EOF) num_lines++;
    	rewind(input);
    
    	//create a 1D array containing the words from the dictionary
    	dictionary=malloc(sizeof(char*)*num_lines);
    	int x;
    	for(x=0;x<num_lines;x++)
    		dictionary[x]=malloc(sizeof(char)*20);
    	num_lines=0;
    	while (fscanf(input,"%s",temp)!=EOF){
    		dictionary[num_lines]=temp;
    		num_lines++;
                    //array properly lists words here
    	}
    
            //but out here all dictionary[x] locations contain the last line in the file.    
    
    	//scan for the input word
    	char *user_Input=NULL;
    	int test;
    	user_Input=malloc(sizeof(char)*20);
    	while(1){
    		printf("Enter a string: ");
    		scanf("%s",user_Input);
    		if(strcmp(user_Input,"exit")==0) break;
    		pthread_t t_1,t_2;
    		if(test = pthread_create(&t_1, NULL, (void *(*)(void *))thread1, user_Input))
    			printf("Thread 1 creation failure.\n");
    		if(test = pthread_create(&t_2, NULL, (void *(*)(void *))thread2, user_Input))
    			printf("Thread 2 creation failure.\n");
    		pthread_join(t_1, NULL);
    		pthread_join(t_2, NULL);
    	}
    	free(user_Input);
    
    
    }
    Thanks for any advice you may have!!!
    Last edited by falcon9; 03-30-2011 at 01:44 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Why don't you use realloc() rather than reading file twice?

    Code:
    char temp[100];
    for(i = 0; i < num_lines;i++) {
      fscanf(input,"%99s",temp);
      dictionary[i] = strdup( temp );
    }
    Code:
    		dictionary[num_lines]=temp;
    is Wrong. Read Question 7.4
    Perhaps you are thinking = does copying ?
    1st you allocate space. the pointer is pointing to that memory, later you point that pointer to temp. Thus your allocated memory is gone. you've no way to access allocated memory.(memory leak).
    Correct way is to copy contents to allocated memory that pointer is pointing.
    Code:
    char *p = malloc( strlen(s) + 1 );      // +1 for '\0'
    p = s;            // Wrong, 
    strcpy(p,s);      // copy s to p, correct
    Last edited by Bayint Naung; 03-30-2011 at 01:35 AM.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4

    Smile

    Problem solved!

    Thanks!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 03:08 AM
  2. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 12:48 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. file pointers + recursion = sore head
    By korbitz in forum C Programming
    Replies: 4
    Last Post: 12-18-2001, 04:55 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

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