Author Topic: Lists in C  (Read 4375 times)

0 Members and 1 Guest are viewing this topic.

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Lists in C
« on: May 17, 2010, 08:40:19 pm »
Hi.  I am having trouble writing a program in C for the TI-89.  I am very new to the language and I still have a lot to learn.

In TI-Basic, I have programs that  can take a list, ex: {1,4,3,1,7,9}, and will retrive a certain element from the list.  For example, if I called for element number 2, the function would return 4.

Is there any way I can get the same feature in C.  Would I use "lists" like in TI-Basic, or is there a different method.  Also, I would have the lists pre-entered in the program.  The program would not have to add data to the lists.


Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: Lists in C
« Reply #1 on: May 17, 2010, 09:00:09 pm »
Lets say I want a list of integers, length 6.
I'd declare it like so:
Code: [Select]
int list[6] = {1, 4, 4, 1, 7, 9}and I'd access the 2nd element with:
Code: [Select]
list[1]since arrays in C are indexed from 0.

Offline apcalc

  • The Game
  • CoT Emeritus
  • LV10 31337 u53r (Next: 2000)
  • *
  • Posts: 1393
  • Rating: +120/-2
  • VGhlIEdhbWUh (Base 64 :))
    • View Profile
Re: Lists in C
« Reply #2 on: May 24, 2010, 06:22:51 pm »
I have another problem with lists in C.  How to I add strings to a list?  For example, I want the list

Code: [Select]
testlist[4]={"Test","List","Hello","World"};
How would I create this list.  I tried declaring it as a character:

Code: [Select]
char testlist[4]={"Test","List","Hello","World"};
but this did not work. What am I doing wrong?  Thanks again for your help! :)


Offline bwang

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 634
  • Rating: +30/-11
    • View Profile
Re: Lists in C
« Reply #3 on: May 24, 2010, 06:39:32 pm »
char* testlist[4] is the declaration you need, I believe.