LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2006, 09:19 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Question Simple array/pointer question.


If I declare a character array and want to add an item to the array, without having it point to that item, for example:

Code:
Code:
char * arr[50];
char temp[255];

for(loop where temp is always changing)
{
  array[i] = temp;
}
for(loop through array)
  printf("%s", array[i]);
Results:
Code:
same
same
same
same
How can I make it so that all elements in the array do not point to the current temp value, but point to what temp was at that time? I know its a pointer problem, but I don't really understand pointers that well.
 
Old 03-14-2006, 09:57 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
this is C i presume?

First of all, do you understand how strings work in C?

temp is a pointer to the start of an array of 255 chars. I don't know what you're doing in the loop (you don't show us), but I'm guessing temp itself never changes; it's the contents of the array that you change. So if you want to keep the contents of that array for later, you're going to have to copy it somewhere. The "array[i] = temp" line makes each array[i] point to the same place that temp points to, but it does not copy the contents of the array.
 
Old 03-15-2006, 08:14 AM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
for each ptr you will use in arr you will need to allocate memory and then copy the contents of temp to that new memory.

dont forget to free your memory when you are done.
 
Old 03-15-2006, 10:15 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
...
array|i]=strdup(temp);
...
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
number of element in a array of pointer? os2 Programming 7 03-09-2005 11:50 AM
about array and pointer seeLnd Programming 5 05-30-2004 07:56 AM
sending pointer array to function marek Programming 4 04-15-2004 04:46 PM
pointer to a 2D array dhanakom Programming 7 09-07-2003 04:22 AM
Assigning a string to a variable (not a pointer, not a array) JStew Programming 3 11-18-2002 08:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration