Thread: Reading words into double pointer

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    16

    Reading words into double pointer

    I have a double pointer, and I need to read words into it. Here's what I have so far:
    Code:
    while(fscanf(keywordFile,"%s",keyinputter)==1){    
      keys_cnt++;
    }
      keywords = (char**)calloc(keys_cnt,sizeof(char));
      rewind(input);
      int y=0;
      while(fscanf(keywordFile,"%s",keyinputter)==1){
        keywords[y] = strdup(keyinputter);
        y++;
      }
    printf("%s\n",keywords[0]);
    keyinputter is an array of size 30. The first while loop finds the number of words in a file so that I can calloc the correct amount of space for the double pointer, keywords. The second while loop is supposed to read the words into the double pointer, but when I run a print statement to print the string at keywords[0], it only prints null and not the word. Am I assigning the strings to the double pointer in the wrong way?
    Last edited by Otto45; 04-22-2013 at 09:10 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    keys_cnt is counting the words, rather than the number of chars. So your allocation is for the number of words, not the length of one word.

    Your logic should be:
    count the number of words

    allocate the first array of pointers (1st dimension)

    rewind the file pointer

    read the first word into a temp string variable - not the array yet

    get the length of that string (why not strlen()?)

    allocate the length of that string + 1 for the end of string char to be put there
    (the 2nd dimension)

    copy the word from the temp string variable, into the array of pointers

    Make sense?

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    16
    Yes, that makes sense. How would I allocate only the first dimension individually, and same question for the second? Would allocating for the first just be:
    Code:
    keywords = (char*)calloc(keys_cnt,sizeof(char))
    and 2nd dimension would be:
    Code:
    keywords[y] = (char**)malloc(strlen()+1)

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Otto45 View Post
    Yes, that makes sense. How would I allocate only the first dimension individually, and same question for the second? Would allocating for the first just be:
    Code:
    keywords = (char*)calloc(keys_cnt,sizeof(char))
    and 2nd dimension would be:
    Code:
    keywords[y] = (char**)malloc(strlen()+1)
    What's wrong with malloc()? The end of string char marks the end of every word, and there is nothing to "calloc()".

    sizes should be key_cnt * sizeof(char*) I have no idea why you named it key_cnt

    malloc() returns a void pointer, so there is no need to cast the return from it, to any type - it's done for you. (and oddly, it can hide an important error).

    Give that a try.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I recommend working throuh a tutorial like A Tutorial on Pointers and Arrays in C before you continue with your program.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double liked list and double pointer
    By saillesh.sabari in forum C Programming
    Replies: 1
    Last Post: 12-10-2010, 11:03 AM
  2. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  3. reading words into set
    By AJOHNZ in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2009, 06:13 PM
  4. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  5. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM