#107658 - spinal_cord - Tue Oct 31, 2006 11:26 pm
Im trying to use palibs file system (cant get fat working yet) to load an external level for my game. the file system can give me a pointer to my .txt file wich contanes my level map. Can someone tell me how to copy the map to an array? I'm quite new to C and I know little to nothing about pointers.
the pointer I would use is PA_PAFSFile(0) and I would like to get info from her to my map array levelloaded[].
Thankyou.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#107662 - sajiimori - Tue Oct 31, 2006 11:52 pm
There's no use in us repeating what is explained better elsewhere. Buy a good C book, such as the C Primer Plus, or just follow some C tutorials on the internet if you're short on time or money.
#107665 - spinal_cord - Wed Nov 01, 2006 12:14 am
ahh yes, but what is the point in a public forum, if not to help each other and share knowlege? And what is the point in there being a c/c++ sub-forum on this forum if the only answers you will get is buy a good book or look else where? in my opinion it seems pointless to have this whole sub-forum if that is the case.
Besides google gives me loads of pages about pointing to arrays, but nothing for copying what the pointer is pointing to to an array.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#107666 - keldon - Wed Nov 01, 2006 12:17 am
spinal_cord wrote: |
ahh yes, but what is the point in a public forum, if not to help each other and share knowlege? And what is the point in there being a c/c++ sub-forum on this forum if the only answers you will get is buy a good book or look else where? in my opinion it seems pointless to have this whole sub-forum if that is the case. |
Don't take offence in what he's saying. Think of it in this way, it would be pointless in having 1000 different forums each duplicating information that already exists. Go to the beginners FAQ, it should have some good cpp tutorials to look at. If anything I would guess that you might be directed to a place like this ( http://www.cplusplus.com/ )
#107669 - sajiimori - Wed Nov 01, 2006 12:32 am
If you "know little to nothing about pointers," then you should learn something about pointers by following some tutorials or reading a book. Every C programmer had to do it, you know? There's no shame in that -- pointers are definitely not obvious at first!
#107673 - sgeos - Wed Nov 01, 2006 2:32 am
spinal_cord wrote: |
ahh yes, but what is the point in a public forum, if not to help each other and share knowlege? |
The point of public forums certainly is to help people.
spinal_cord wrote: |
And what is the point in there being a c/c++ sub-forum on this forum if the only answers you will get is buy a good book or look else where? |
To answer questions that are not covered by the book. The C/C++ forum assumes that you have at least basic, if not advanced, C/C++ programming ability. It also assumes that you are comfortable with the GBA hardware.
The beginners forum only assumes programming ability.
spinal_cord wrote: |
in my opinion it seems pointless to have this whole sub-forum if that is the case. |
All posters are basically expected to have given a best effort attempt before posting. When you post, tell us what you did, what you expected to happen, and what didn't happen. We'll help. We won't do your work for you.
I, for one, also expect people to put effort into spelling and grammar.
spinal_cord wrote: |
Besides google gives me loads of pages about pointing to arrays, but nothing for copying what the pointer is pointing to to an array. |
Give it a shot on your own. Try a few things even if you expect them all to fail. You might be able to figure it out on your own. If not, tell us what you did, what you expected to happen, and what did or didn't happen. We'll help.
The comp.lang.c FAQ might be able to help you. It assumes basic C programming ability. I learned C from Practical C Programming, by Steve Oualline. There are probably better books out there.
-Brendan
#107769 - KayH - Wed Nov 01, 2006 9:22 pm
More detailed you find the things said above:
http://catb.org/esr/faqs/smart-questions.html
This is not the topic here, but usefull to know in any case.
Working with pointers is essential for a C/C++ programmer. Before you can goon,
read and understand the things shown in a good tutorial! Otherwise the community can't help you.
HTH KaY
#107805 - Ant6n - Thu Nov 02, 2006 5:21 am
try
http://pw1.netcom.com/~tjensen/ptr/pointers.htm
there is a strong correlation between pointers and arrays, and learning c involves learning about pointers (its like when you learn java you learn what classes are, and when you learn sml you learn about higher order functions etc)
#107829 - spinal_cord - Thu Nov 02, 2006 12:06 pm
I now know a little about pointers, but i must still ask my origional question -
How do I copy the srting that my pointer is pointing at, to an array.
myarray[100]=mypointer only makes myarray[100] a copy of the string (or just another pointer to it?) how can i make myarray[0] to the first value in the string, eg. 'A' then myarray[1] to the second etc.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#107831 - sgeos - Thu Nov 02, 2006 12:32 pm
I'll give you a hint. A string is an array of characters.
-Brendan
#107837 - gmiller - Thu Nov 02, 2006 2:17 pm
If you have a point to a type and you want the j'th element of that type then just use the pointer and subscript it. Array names are just pointers to the type of the array element. Here are some examples:
Code: |
int junk[100]; // reserve space for 100 int's
short *foo = (short *)junk; // point the short * to the junk array
/*
Of course I use the higher alignment requirement for int's to make sure I
do not get into trouble with the short pointer.
*/
foo[5] = 12; // set the 5th short element to 12
*(foo + 5) = 12; // set the 5th short element = 12;
*junk = 77; // set the zero'th int element to 77
junk[0] = 77; // set the zero'th int element to 77
/*
This code is just taking advantage of the array name = pointer to element
but it was meant to show an example. The overlap of the shorts will be
that the the first two shorts (assuming 16 bit) overlap the first int
(assuming 32 bit). Depending on the machines endian-ness will
determine if the first short is the high order bytes of the int or low
order bytes.
*/
|
#107897 - spinal_cord - Thu Nov 02, 2006 11:22 pm
sgeos wrote: |
I'll give you a hint. A string is an array of characters.
-Brendan |
not much of a hint, i know what a string is, and should you have told me it ends with a \0 ?
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#107898 - keldon - Thu Nov 02, 2006 11:32 pm
spinal_cord wrote: |
sgeos wrote: | I'll give you a hint. A string is an array of characters.
-Brendan |
not much of a hint, i know what a string is, and should you have told me it ends with a \0 ? |
It really is a big hint depending on your knowledge.
Code: |
char *ptr;
ptr = &myArray[100]; |
#107901 - gmiller - Thu Nov 02, 2006 11:45 pm
Well the example you gave set the pointer to the address of the 100'th element in the array and I do not know if that's what you meant.
#107906 - keldon - Fri Nov 03, 2006 12:34 am
Sorry, I'm quite busy preparing for a session tomorrow so I'm just throwing in nuggets where I think you can fill in the blanks. But in short I thought [from what you said] that you wanted to copy text from a string starting from a particular character, say the 100th. By starting the pointer at the 100th element you can then copy until the termination and be able to copy the substring.
I'm not sure exactly what problem you are experiencing.
#107912 - chishm - Fri Nov 03, 2006 1:54 am
Have a look at strncpy. If I understood your question correctly, you want something like:
Code: |
strncpy (myarray, mypointer, 100); |
This will copy up to 100 characters from the location mypointer points at to the location myarray points at. If there are less than 100 characters in the string mypointer, myarray will only contain that many characters and the rest of the space in myarray will have whatever was in there before. Keep in mind that if mypointer has 100 or more characters, myarray won't be null terminated.
To be on the safe side, you should do it similar to this:
Code: |
/* It's better to use constants rather than magic numbers */
#define MYARRAY_LENGTH 100
char myarray [MYARRAY_LENGTH];
/* Copy the string, reserving one char for the null terminator */
strncpy (myarray, mypointer, MYARRAY_LENGTH - 1);
/* Set the last character of myarray to null, to make sure the string is always terminated correctly. Notice how MYARRAY_LENGTH - 1 is used instead of MYARRAY_LENGTH. This is because arrays are indexed starting from 0, so the 100th character of myarray is at myarray[99] */
myarray [MYARRAY_LENGTH - 1] = '\0'; |
If this doesn't help, I suggest you read a good C or C++ book, specifically the sections on arrays, pointers and strings.
_________________
http://chishm.drunkencoders.com
http://dldi.drunkencoders.com
#107944 - sgeos - Fri Nov 03, 2006 10:14 am
spinal_cord wrote: |
not much of a hint, i know what a string is, and should you have told me it ends with a \0 ? |
Without knowing what you are having trouble with, it is difficult to help you. You've asked a question that any basic C book will answer.
spinal_cord wrote: |
How do I copy the srting that my pointer is pointing at, to an array. |
If you must be given the answer, I'd use strcpy. If for whatever reason strcpy doesn't meet your needs, I'd use a loop.
-Brendan
#107956 - kusma - Fri Nov 03, 2006 12:45 pm
gmiller wrote: |
Well the example you gave set the pointer to the address of the 100'th element in the array and I do not know if that's what you meant. |
actually, the 101'th in our normal 1-based numbering system ;)
#107957 - gmiller - Fri Nov 03, 2006 1:00 pm
kusma wrote: |
gmiller wrote: | Well the example you gave set the pointer to the address of the 100'th element in the array and I do not know if that's what you meant. |
actually, the 101'th in our normal 1-based numbering system ;) |
Yep ... I screwed up ... 101'th my bad
#108034 - spinal_cord - Sat Nov 04, 2006 5:55 pm
chishm - that worked fine, thakyou, I realy must get a better book.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage