943,576 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 800
  • C RSS
Jan 6th, 2009
0

Problem with array of pointers

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brimble2010 is offline Offline
6 posts
since Jan 2009
Jan 6th, 2009
0

Re: Problem with array of pointers

> linep = (char *)&line;
Try strcpy() instead.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 6th, 2009
0

Re: Problem with array of pointers

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brimble2010 is offline Offline
6 posts
since Jan 2009
Jan 6th, 2009
0

Re: Problem with array of pointers

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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brimble2010 is offline Offline
6 posts
since Jan 2009
Jan 6th, 2009
1

Re: Problem with array of pointers

>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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 6th, 2009
0

Re: Problem with array of pointers

Click to Expand / Collapse  Quote originally posted by Aia ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brimble2010 is offline Offline
6 posts
since Jan 2009
Jan 6th, 2009
0

Re: Problem with array of pointers

  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.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Jan 6th, 2009
0

Re: Problem with array of pointers

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 7th, 2009
0

Re: Problem with array of pointers

thanks for all the advice everyone, helps no end
Reputation Points: 10
Solved Threads: 0
Newbie Poster
brimble2010 is offline Offline
6 posts
since Jan 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Optimized quicksort implementation
Next Thread in C Forum Timeline: structure and reading a text file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC