|
|
|||||||
| About Us | Register | FAQ | Members List | Calendar | Mark Forums Read |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
General of Tangerines
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2001
Location: Defending the Sea
Posts: 3,885
|
This program is suppose to get the inputs and place the inputs into a pointer array.
Eg. INPUT: Hello Bye OUTPUT: Hello Bye This is what is occurring: INPUT: Hello Bye OUTPUT: Bye Bye Can someone please help me! #include stdio.h (Can't show <> for some reason) int main() { char line[20]; char *list[50]; int counter=0,count; gets(line); list[counter]=line; counter=counter+1; gets(line); list[counter]=line; for (count=0;count<=counter;count++) { puts (list[count]); } return 0; } |
|
|
|
|
|
#2 (permalink) |
|
.:: PHP King ::.
![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Bahrain
Posts: 580
|
do the following because when you change line in the second time the pointer list[counter] is point to line and line is changed
int main() { char line[20]; char line2[20]; char *list[50]; int counter=0,count; gets(line); list[counter]=line; counter=counter+1; gets(line2); list[counter]=line2; for (count=0;count<=counter;count++) { puts (*(list+count)); } return 0; } |
|
|
|
|
|
#3 (permalink) | |
|
General of Tangerines
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2001
Location: Defending the Sea
Posts: 3,885
|
Quote:
#include stdio.h (Can't show <> for some reason) int main() { char line[20]; char *list[50]; int counter=0,count; for (counter=0;counter<2;counter++) { printf("Enter Line: %d",count); gets(line); list[counter]=line; } for (count=0;count<=counter;count++) { puts (list[count]); } return 0; } Anyway to work around that? |
|
|
|
|
|
|
#4 (permalink) |
|
Emu author
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Vienna/Austria/Europe
Posts: 1,168
|
Well, a pointer array is just an array of pointers. But you need something else: Space to store your data. A pointer is simply an address, not the space where you store your data. Easiest solution: Use a string array instead of a pointer array.
#include <stdio.h> // (Can show <> for a good reason ;-) int main() { char list[50][20]; int counter=0,count; counter=0; // you would normaly place some kind of loop here gets(list[counter++]); gets(list[counter++]); // end loop for (count=0;count<counter;count++) { puts (list[count]); } return 0; }
__________________
If you think my English is bad, wait till you read my Polish. Last edited by ammoQ; November 21st, 2002 at 19:43. |
|
|
|
|
|
#5 (permalink) |
|
Emu author
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Vienna/Austria/Europe
Posts: 1,168
|
Now the solution with pointers:
#include <stdio.h> // (Can show <> for a good reason ;-) #include <stdlib.h> int main() { char *list[50]; int counter=0,count; counter=0; // you would normaly place some kind of loop here list[counter] = malloc(20); // get some memory gets(list[counter++]); list[counter] = malloc(20); gets(list[counter++]); // end loop for (count=0;count<counter;count++) { puts (list[count]); free(list[count]); // give back the memory } return 0; }
__________________
If you think my English is bad, wait till you read my Polish. |
|
|
|
|
|
#6 (permalink) |
|
.:: PHP King ::.
![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Bahrain
Posts: 580
|
Thanks ammoQ, I was looking for another use of function malloc (memory allocation).
by the way in my study we didn't discuss function malloc yet so if this is my assingment my teacher will not accept it with malloc function. |
|
|
|
|
|
#7 (permalink) |
|
Emu author
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Mar 2002
Location: Vienna/Austria/Europe
Posts: 1,168
|
Without malloc, you have to pre-allocate memory in an array, like in my first example. Note: all those examples are totaly unsafe in terms of buffer overflows etc. There is no way to use gets() safely, it would be better to use fgets(s, 20, stdin) instead.
__________________
If you think my English is bad, wait till you read my Polish. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|