Problem with array of pointers

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 5
Reputation: brimble2010 is an unknown quantity at this point 
Solved Threads: 0
brimble2010 brimble2010 is offline Offline
Newbie Poster

Problem with array of pointers

 
0
  #1
Jan 6th, 2009
Hi (first post ),
I have a coursework where we have to read in 50 lines from a file (bubble.txt), sort them into length order and then print them out (its not the whole coursework, just a part). I thought that the only way of holding lines of text in an array is to hold a pointer to each char array in an array called strings.

For some reason (I must admit, pointer do confuse me a bit), whenever I run this code below (just to make sure the lines are in the array) i keep getting the last line (line 50) printed 50 times and I can't tell why.

Any help appreciated.

Kristian Brimble

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "string.h"
  5.  
  6. char *strings[50];
  7.  
  8. void readLines(void);
  9. void printLines(void);
  10.  
  11. int main()
  12. {
  13. readLines();
  14. printLines();
  15. return 0;
  16. }
  17.  
  18. void readLines(void)
  19. {
  20. FILE *inputFilePtr;
  21. inputFilePtr = fopen("bubble.txt", "r");
  22. char line[1000];
  23.  
  24. for(int i = 0; i < 50; i++)
  25. {
  26. fgets(line, 1000, inputFilePtr);
  27. char *linep = malloc(sizeof(char) * 1000);
  28. linep = (char *)&line;
  29. strings[i] = linep;
  30. }
  31. }
  32.  
  33. void printLines(void)
  34. {
  35. for(int i = 0; i < 50; i++)
  36. {
  37. printf("%s", strings[i]);
  38. }
  39. }
Last edited by brimble2010; Jan 6th, 2009 at 4:26 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Problem with array of pointers

 
0
  #2
Jan 6th, 2009
> linep = (char *)&line;
Try strcpy() instead.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: brimble2010 is an unknown quantity at this point 
Solved Threads: 0
brimble2010 brimble2010 is offline Offline
Newbie Poster

Re: Problem with array of pointers

 
0
  #3
Jan 6th, 2009
Originally Posted by Salem View Post
> linep = (char *)&line;
Try strcpy() instead.
worked a treat, thank you.

any chance you could explain what I was doing and what strcpy() does differently, just for future reference?

thanks again
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: brimble2010 is an unknown quantity at this point 
Solved Threads: 0
brimble2010 brimble2010 is offline Offline
Newbie Poster

Re: Problem with array of pointers

 
0
  #4
Jan 6th, 2009
also, i have added the bubble sort to the code but it isnt swapping the pointers stored in the array and i know it is something to do with the swap method, but i just cant see what it is.

again any help very much appreciated

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. char *strings[50];
  6.  
  7. void readLines(void);
  8. void sortLines(void);
  9. void swapLines(int j, int k);
  10. void printLines(void);
  11.  
  12. int main()
  13. {
  14. readLines();
  15. sortLines();
  16. printLines();
  17. return 0;
  18. }
  19.  
  20. void readLines(void)
  21. {
  22. FILE *inputFilePtr;
  23. inputFilePtr = fopen("bubble.txt", "r");
  24. char line[1000];
  25.  
  26. for(int i = 0; i < 50; i++)
  27. {
  28. fgets(line, 1000, inputFilePtr);
  29. char *linep = malloc(sizeof(char) * 1000);
  30. strcpy(linep, line);
  31. strings[i] = linep;
  32. }
  33. }
  34.  
  35. void sortLines(void)
  36. {
  37. for(int i = 0; i < 50; i++)
  38. {
  39. for(int j = 0, k = 0; k < 50; j++, k++)
  40. {
  41. int jLen = strlen(strings[j]);
  42. int kLen = strlen(strings[k]);
  43. if(jLen >= kLen)
  44. {
  45. swapLines(j, k);
  46. }
  47. }
  48. }
  49. }
  50.  
  51. void swapLines(int j, int k)
  52. {
  53. char *temp = malloc(sizeof(char) * 1000);
  54.  
  55. temp = strings[j];
  56. strings[j] = strings[k];
  57. strings[k] = temp;
  58. }
  59.  
  60. void printLines(void)
  61. {
  62. for(int i = 0; i < 50; i++)
  63. {
  64. printf("%s", strings[i]);
  65. }
  66. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Problem with array of pointers

 
1
  #5
Jan 6th, 2009
>any chance you could explain what I was doing and what strcpy() does differently, just for future reference?

  1. char *linep = malloc(sizeof(char) * 1000);
malloc sets apart the memory you specified. And you keep track of that memory via the pointer linep

But that pointer can be reassigned to point to some other piece of memory. That's what you are forcing it to do here.
  1. linep = (char *)&line;
I said forcing it, because you are casting it.
Like this linep = &line ; would have gave you an error.
What where you pointing to? To the memory that fgets() used to store the last read string. line[1000]; And then, you stored the same location address, in fifty different pointers. You accomplished that with strings[i] = linep; .
When you printf'ed each one of these pointers, all of them yielded the same address, therefore, the last string line read was displayed.

Moral of the story? A pointer is not a string. A string is not a variable.
There's not such a thing as string variable in C, you make a string by joining together many single char variables and terminating it with the '\0'. That's all that keeps the string as a string. A fragile single '\0'.
strcpy() knows how to copy each and every one of this single chars terminating it with '\0', into a piece of memory that you have allocated before hand.
Last edited by Aia; Jan 6th, 2009 at 7:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: brimble2010 is an unknown quantity at this point 
Solved Threads: 0
brimble2010 brimble2010 is offline Offline
Newbie Poster

Re: Problem with array of pointers

 
0
  #6
Jan 6th, 2009
Originally Posted by Aia View Post
What where you pointing to? To the memory that fgets() used to store the last read string. line[1000]; And then, you stored the same location address, in fifty different pointers. You accomplished that with strings[i] = linep; .
When you printf'ed each one of these pointers, all of them yielded the same address, therefore, the last string line read were displayed.
Thanks Aia, cleared up a lot for me, esp the quoted part

Glad i signed up now
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1,438
Reputation: William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of William Hemsworth has much to be proud of 
Solved Threads: 118
Sponsor
William Hemsworth William Hemsworth is online now Online
Nearly a Posting Virtuoso

Re: Problem with array of pointers

 
0
  #7
Jan 6th, 2009
  1. void swapLines(int j, int k)
  2. {
  3. char *temp = malloc(sizeof(char) * 1000);
  4. temp = strings[j]; // What happened to the 1000 chars you just allocated?
  5. strings[j] = strings[k];
  6. strings[k] = temp;
  7. }
Memory leak!

Remember to free any data you allocate using the free function.
Last edited by William Hemsworth; Jan 6th, 2009 at 7:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Problem with array of pointers

 
0
  #8
Jan 6th, 2009
A few other things you need to learn before you can make that piece of code work properly.

Every time you are about to allocate dynamic memory (like with malloc), you need to keep in mind two things. First, you need to check that the setting apart of that memory was successful.
  1. char *linep = malloc(sizeof(char) * 1000);
  2. if ( linep == NULL) {
  3. /* if you got here, memory was not successfully allocated */
  4. /* you need to handle the error in a proper way, but you can't */
  5. /* count with that memory */
  6. }

And second. You need to free that memory when you are finished, using it.
  1. free(linep);
These two things are a must, every time you make use of dynamic memory.

As a bonus. I can let you know about a third concept that you need to be aware.
If you reassign a pointer from dynamic memory like linep = line; (that would have been the correct way of doing linep = (char *)&line; ), that memory previously allocated is lost for you to use. You don't have a way of getting to it, and it will stay allocated to the program until the program is closed, if the operating system is smart to release it, afterward.
As you can see, if you put that in a loop, you could waste a lot of memory pretty easily.

  1. inputFilePtr = fopen("bubble.txt", "r");
When you want to open a file to work on it or read from it, you need to check that it was successful in doing so. Kind of the same that with malloc(). You computer can not work with that file if it could not open it, can't it?
Last edited by Aia; Jan 6th, 2009 at 7:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 5
Reputation: brimble2010 is an unknown quantity at this point 
Solved Threads: 0
brimble2010 brimble2010 is offline Offline
Newbie Poster

Re: Problem with array of pointers

 
0
  #9
Jan 7th, 2009
thanks for all the advice everyone, helps no end
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC