Thread: need help with pointers to a list of chars

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    22

    need help with pointers to a list of chars

    Can I treat a pointer to a list of chars, just like an array in C++? I have an assignment, he gave us a defined list of pointers to chars.
    Code:
    	static char *list[100] = { "the", "of", "and", "a", "to", "in", "is", "you",
    	"that", "it", "he", "was", "for", "on", "are", "as", "with", "his", "they",
    	"I", "at", "be", "this", "have", "from", "or", "one", "had", "by",
    	"word", "but", "not", "what", "all", "were", "we", "when", "your", "can",
    	"said", "there", "use", "an", "each", "which", "she", "do", "how", "their",
    	"if", "will", "up", "other", "about", "out", "many", "then", "them", "these",
    	"so", "some", "her", "would", "make", "like", "him", "into", "time", "has",
    	"look", "two", "more", "write", "go", "see", "number", "no", "way", "could",
    	"people", "my", "than", "first", "water", "been", "call", "who", "oil",
    	"its", "now", "find", "long", "down", "day", "did", "get", "come", "made",
    	"may", "part"};
    My question is I used a random number generator to get random numbers to pull from the list, I thought just doing this would work:
    Code:
    	printf("%s\n", list[rand] );
    How could I use the random number generator to index the array?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Not only does the above behave like an array, it is an array. (Specifically, an array of char *, each of which is then initialized to point to some convenient part of memory where those strings have been placed.)

    My Hat of Guessing says that you need to use () to make your compiler believe that rand is a function, i.e., rand(). You also need to make sure that you scale your random number to be in [0,100); that's an FAQ, but you should just be able to mod by 100 and be done with it.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by tabstop View Post
    Not only does the above behave like an array, it is an array. (Specifically, an array of char *, each of which is then initialized to point to some convenient part of memory where those strings have been placed.)

    My Hat of Guessing says that you need to use () to make your compiler believe that rand is a function, i.e., rand(). You also need to make sure that you scale your random number to be in [0,100); that's an FAQ, but you should just be able to mod by 100 and be done with it.
    Depending on how random things need to be in this particular case, you might want to try another approach than % to get the number into range.

    http://www.eternallyconfuzzled.com/a..._art_rand.aspx
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    At the very least, make the array const
    Code:
    	static const char* list[100] = { "the", "of", "and", "a", "to", "in", "is", "you",
    	"that", "it", "he", "was", "for", "on", "are", "as", "with", "his", "they",
    	"I", "at", "be", "this", "have", "from", "or", "one", "had", "by",
    	"word", "but", "not", "what", "all", "were", "we", "when", "your", "can",
    	"said", "there", "use", "an", "each", "which", "she", "do", "how", "their",
    	"if", "will", "up", "other", "about", "out", "many", "then", "them", "these",
    	"so", "some", "her", "would", "make", "like", "him", "into", "time", "has",
    	"look", "two", "more", "write", "go", "see", "number", "no", "way", "could",
    	"people", "my", "than", "first", "water", "been", "call", "who", "oil",
    	"its", "now", "find", "long", "down", "day", "did", "get", "come", "made",
    	"may", "part"};
    Since these strings are non-modifiable.
    I'm not sure if this is the correct syntax or if it should be some more; these things are typically complex and I don't use them very often...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    22
    I'm still getting errors when I go to compile I changed the thing you all have suggested, so now it reads:
    Code:
    	static const char *list[100] = { "the", "of", "and", "a", "to", "in", "is", "you",
    	"that", "it", "he", "was", "for", "on", "are", "as", "with", "his", "they",
    	"I", "at", "be", "this", "have", "from", "or", "one", "had", "by",
    	"word", "but", "not", "what", "all", "were", "we", "when", "your", "can",
    	"said", "there", "use", "an", "each", "which", "she", "do", "how", "their",
    	"if", "will", "up", "other", "about", "out", "many", "then", "them", "these",
    	"so", "some", "her", "would", "make", "like", "him", "into", "time", "has",
    	"look", "two", "more", "write", "go", "see", "number", "no", "way", "could",
    	"people", "my", "than", "first", "water", "been", "call", "who", "oil",
    	"its", "now", "find", "long", "down", "day", "did", "get", "come", "made",
    	"may", "part"};
    .
    .
    .
    		printf("%s\n", list[rand()] );
    I still get errors they say, " HW#1b.c:46: error: ‘list’ undeclared (first use in this function)
    HW#1b.c:46: error: (Each undeclared identifier is reported only once
    HW#1b.c:46: error: for each function it appears in.)" I'm not sure what I'm doing wrong. I just want to use the random number generator to index the array, so I can pull different (random) words out of it. How would I do this?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I think you're going to need to post more code; perhaps the entire program.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    These errors stem from that list isn't defined properly, but there's no error saying why list isn't defined properly. Are you sure that's all errors?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    you have more than 100 elements. static const char *list[] = { "the", "of", ...

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My Hat of Guessing now thinks that you have declared your static const char * [] inside a function, and are trying to access it from a different function. The static keyword just means that the pointer array exists throughout the entire program; the name, however, is still only known inside the function itself.

    But, I am just pulling all this out of my hat; if you want explicit help you're going to have to be explicit with the problem.

    PS: Rob, did you actually count all those? You're a better man than me.... Edit: So I had to go count the words, twice, and each time I came up with 100. I think I've been had.
    Last edited by tabstop; 01-13-2008 at 06:37 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    hmm... I got about 110 the first time around.

    eh... think I'll blame it on the internet.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    That "Hat of Guessing" seems like it could be a handy item. Where might I acquire one? Wal-Mart or Hogwarts?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rags_to_riches View Post
    That "Hat of Guessing" seems like it could be a handy item. Where might I acquire one? Wal-Mart or Hogwarts?
    Think more "Whose Line Is It Anyway?".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting linked list please help with CODE
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 09-27-2008, 11:24 PM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 12:30 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM