Emuforums.com

Go Back   Emuforums.com > General Discussion > Web development / Programming
About Us Register FAQ Members List Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old November 21st, 2002   #1 (permalink)
General of Tangerines
 
RZetlin's Avatar
 
Join Date: Jun 2001
Location: Defending the Sea
Posts: 3,885
Unhappy C Programming Help (Pointers/Arrays)

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;
}
__________________


AMD Athlon 64 3700+ | 2 GB RAM | XFX Nvidia 6800 GS 256 MB XXX Edition | Win XP Pro SP2
RZetlin is offline   Reply With Quote
Old November 21st, 2002   #2 (permalink)
.:: PHP King ::.
 
Mohd's Avatar
 
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;
}
Mohd is offline   Reply With Quote
Old November 21st, 2002   #3 (permalink)
General of Tangerines
 
RZetlin's Avatar
 
Join Date: Jun 2001
Location: Defending the Sea
Posts: 3,885
Quote:
Originally posted by squall84
do the following because when you change line in the second time the pointer list[counter] is point to line and line is changed
Hmm...that won't work if the input is in a for loop.

#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?
__________________


AMD Athlon 64 3700+ | 2 GB RAM | XFX Nvidia 6800 GS 256 MB XXX Edition | Win XP Pro SP2
RZetlin is offline   Reply With Quote
Old November 21st, 2002   #4 (permalink)
Emu author
 
ammoQ's Avatar
 
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 &lt;stdio.h&gt; // (Can show &lt;&gt; 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&lt;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.
ammoQ is offline   Reply With Quote
Old November 21st, 2002   #5 (permalink)
Emu author
 
ammoQ's Avatar
 
Join Date: Mar 2002
Location: Vienna/Austria/Europe
Posts: 1,168
Now the solution with pointers:

#include &lt;stdio.h&gt; // (Can show <> for a good reason ;-)
#include &lt;stdlib.h&gt;

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&lt;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.
ammoQ is offline   Reply With Quote
Old November 22nd, 2002   #6 (permalink)
.:: PHP King ::.
 
Mohd's Avatar
 
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.
Mohd is offline   Reply With Quote
Old November 22nd, 2002   #7 (permalink)
Emu author
 
ammoQ's Avatar
 
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.
ammoQ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 18:18.

© 2006 - 2008 Emu Forums | About Emu Forums | Legal | A member of the Crowdgather Forum Community


Powered by vBulletin® Version 3.7.0 Release Candidate 3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5