By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,388 Members | 1,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,388 IT Pros & Developers. It's quick & easy.

Problem with pointers/addresses

P: 1
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:
Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.    char word[20];
  4.    char word2[20];
  5. }Words;
Main function:
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3.    Words *my_words;
  4.    int i=0;
  5.    readwords(&my_words);
  6.    for(i=0;i<sizeof(my_words)/sizeof(*my_words);i++)
  7.    {
  8.       printf("%s is word\n", my_words[i].word);
  9.       printf("%s is word2\n", my_words[i].word2);
  10.    }
  11. }
readWords function:
Expand|Select|Wrap|Line Numbers
  1. void readWords(Words *mywords);
  2. {
  3.    int number_of_words=0;
  4.    //here I open file, read number of the words from the file
  5.    if ((my_words=malloc(number_of_words*sizeof(*mywords))) == NULL)
  6.    {
  7.     //report failure
  8.    }
  9.  
  10.    while (feof(file)==0 && j<number_of_words)
  11.    }
  12.       /* here I read the words from a file into my_words[i].word
  13.          and my_words[i].word2 */
  14.    }
  15.  
  16.    for(i=0;i<sizeof(my_words)/sizeof(*my_words);i++)
  17.    {
  18.       printf("%s is word\n", my_words[i].word);
  19.       printf("%s is word2\n", my_words[i].word2);
  20.    }
  21. }
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
Nov 1 '06 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 1,510
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
Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct
  5. {
  6.    char word[20];
  7.    char word2[20];
  8. }Words;
  9.  
  10. // myWords is pointer to a pointer to Word
  11. void readWords(Words **my_words, int number_of_words  )
  12. {
  13.    int i, j;
  14.    //here I open file, read number of the words from the file
  15.    if ((*my_words=malloc(number_of_words*sizeof(*my_words)  )) == NULL)
  16.    {
  17.     //report failure
  18.    }
  19.  
  20.    // put some data in Wods
  21.    for(j=0;j<number_of_words;j++)
  22.      {
  23.      for (i=0;i<19;i++) 
  24.          {(*my_words)[j].word[i]=(*my_words)[j].word2[i]='A'+i+j; }
  25.      (*my_words)[j].word[19]=(*my_words)[j].word2[19]=0;
  26.      }
  27.   for(j=0;j<number_of_words;j++)
  28.   {
  29.       printf("%s is word\n", (*my_words)[j].word);
  30.       printf("%s is word2\n", (*my_words)[j].word2);
  31.    }
  32. }
  33.  
  34. int main()
  35. {
  36.    Words *my_words;
  37.    int i=0, number_of_words=3;
  38.    readWords(&my_words, number_of_words);
  39.  
  40.   for(i=0;i<number_of_words;i++)
  41.   {
  42.       printf("in main() %d %s is word\n", i, my_words[i].word);
  43.       printf("in main() %d %s is word2\n", i, my_words[i].word2);
  44.   }
  45.       return 0;
  46. }
  47.  
alternativly you can return the pointer to the memory allocated by malloc() via the function result
Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. typedef struct
  5. {
  6.    char word[20];
  7.    char word2[20];
  8. }Words;
  9.  
  10. // myWords is pointer to a pointer to Word
  11. Words *  readWords(int number_of_words  )
  12. {
  13.    int i, j;
  14.    Words *my_words;
  15.    //here I open file, read number of the words from the file
  16.    if ((my_words=malloc(number_of_words*sizeof(*my_words)  )) == NULL)
  17.    {
  18.     //report failure
  19.    }
  20.  
  21.    // put some data in Wods
  22.    for(j=0;j<number_of_words;j++)
  23.      {
  24.      for (i=0;i<19;i++) 
  25.          { my_words[j].word[i]=my_words[j].word2[i]='A'+i+j; }
  26.      my_words[j].word[19]=my_words[j].word2[19]=0;
  27.      }
  28.   for(j=0;j<number_of_words;j++)
  29.   {
  30.       printf("%s is word\n", my_words[j].word);
  31.       printf("%s is word2\n", my_words[j].word2);
  32.    }
  33.    return my_words;
  34. }
  35.  
  36. int main()
  37. {
  38.    Words *my_words;
  39.    int i=0, number_of_words=3;
  40.    my_words = readWords(number_of_words);
  41.  
  42.   for(i=0;i<number_of_words;i++)
  43.   {
  44.       printf("in main() %d %s is word\n", i, my_words[i].word);
  45.       printf("in main() %d %s is word2\n", i, my_words[i].word2);
  46.   }
  47.       return 0;
  48. }
  49.  
Nov 1 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.