Problem with pointers/addresses

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Imnj
    New Member
    • Nov 2006
    • 1

    #1

    Problem with pointers/addresses

    Hi, all.

    Here I have little program which should allocate memory dynamically by using malloc and then read data in from files. Then give address of the data to main, but no can do.

    Im coding on WinXP with Bloodshed DEV C++ and MS Visual Express 2005 C++, but code should be C.

    These are stripped down versions of the code, because I think my problem lies within the pointers.

    Here's the struct, plain and simple:
    Code:
    typedef struct
    {
       char word[20];
       char word2[20];
    }Words;
    Main function:
    Code:
    void main()
    {
       Words *my_words;
       int i=0;
       readwords(&my_words);
       for(i=0;i<sizeof(my_words)/sizeof(*my_words);i++)
       {
          printf("%s is word\n", my_words[i].word);
          printf("%s is word2\n", my_words[i].word2);
       }
    }
    readWords function:
    Code:
    void readWords(Words *mywords);
    {
       int number_of_words=0;
       //here I open file, read number of the words from the file
       if ((my_words=malloc(number_of_words*sizeof(*mywords))) == NULL)
       {
    	//report failure
       }
       
       while (feof(file)==0 && j<number_of_words)
       }
          /* here I read the words from a file into my_words[i].word
             and my_words[i].word2 */
       }
       
       for(i=0;i<sizeof(my_words)/sizeof(*my_words);i++)
       {
          printf("%s is word\n", my_words[i].word);
          printf("%s is word2\n", my_words[i].word2);
       }
    }
    So, in readWords everything is working just fine, it's printing the contents of a file ok. But when it gets back to Main, printing my_words results only some weird stuff.

    I checked the addresses with printf("%d" &my_words) and they differ, but I just can't figure out the solution.

    So, what I would like to know is how dynamically reserve memory, put some data in there and return it to Main(or some other function), 'cuz now I just can't do it.

    I've done my swap(&a, &b) excercises and I understand it(I think so;)), but with malloc, I can't get it to work.

    Sorry for my bad english, I hope you guys understand what I'm trying to say here.

    All help is highly appreciated!

    NJ
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    when you malloc() inside the function readWords() you do not effect the value of
    Words *my_words;
    in your main() - you need need to pass in a pointer to *my_words and also the number of words for malloc() to allocate i.e. the readWords() function header should look like
    void readWords(Words **my_words, int number_of_words )

    the program now looks like
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct
    {
       char word[20];
       char word2[20];
    }Words;
    
    // myWords is pointer to a pointer to Word
    void readWords(Words **my_words, int number_of_words  )
    {
       int i, j;
       //here I open file, read number of the words from the file
       if ((*my_words=malloc(number_of_words*sizeof(*my_words)  )) == NULL)
       {
    	//report failure
       }
       
       // put some data in Wods
       for(j=0;j<number_of_words;j++)
         {
         for (i=0;i<19;i++) 
             {(*my_words)[j].word[i]=(*my_words)[j].word2[i]='A'+i+j; }
         (*my_words)[j].word[19]=(*my_words)[j].word2[19]=0;
         }
      for(j=0;j<number_of_words;j++)
      {
          printf("%s is word\n", (*my_words)[j].word);
          printf("%s is word2\n", (*my_words)[j].word2);
       }
    }
    
    int main()
    {
       Words *my_words;
       int i=0, number_of_words=3;
       readWords(&my_words, number_of_words);
    
      for(i=0;i<number_of_words;i++)
      {
          printf("in main() %d %s is word\n", i, my_words[i].word);
          printf("in main() %d %s is word2\n", i, my_words[i].word2);
      }
          return 0;
    }
    alternativly you can return the pointer to the memory allocated by malloc() via the function result
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct
    {
       char word[20];
       char word2[20];
    }Words;
    
    // myWords is pointer to a pointer to Word
    Words *  readWords(int number_of_words  )
    {
       int i, j;
       Words *my_words;
       //here I open file, read number of the words from the file
       if ((my_words=malloc(number_of_words*sizeof(*my_words)  )) == NULL)
       {
    	//report failure
       }
       
       // put some data in Wods
       for(j=0;j<number_of_words;j++)
         {
         for (i=0;i<19;i++) 
             { my_words[j].word[i]=my_words[j].word2[i]='A'+i+j; }
         my_words[j].word[19]=my_words[j].word2[19]=0;
         }
      for(j=0;j<number_of_words;j++)
      {
          printf("%s is word\n", my_words[j].word);
          printf("%s is word2\n", my_words[j].word2);
       }
       return my_words;
    }
    
    int main()
    {
       Words *my_words;
       int i=0, number_of_words=3;
       my_words = readWords(number_of_words);
    
      for(i=0;i<number_of_words;i++)
      {
          printf("in main() %d %s is word\n", i, my_words[i].word);
          printf("in main() %d %s is word2\n", i, my_words[i].word2);
      }
          return 0;
    }

    Comment

    Working...