Thread: Finding a character in an array of strings

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    Finding a character in an array of strings

    I'm pretty new to C and I'm trying to figure out how to access a specific character in a specific string in an array of strings. For example, I've got an array of strings, rec[2], which contains four strings. They can be anything but let's say:

    rec[0] = test1
    rec[1] = test2
    rec[2] = test3

    I'd like to be able to reference the fifth character in each of the strings in the array. Here's what I've got, but it's not working. Note that the "rec" array has been passed into this function and I'm able to reference whole strings, but not specific characters.

    Code:
    int i;
    
    for(i = 0; i < 3; i++) {
    	printf("%s is test number %d\n", rec[i][4], i+1);
    }
    I thought that something like rec[0][4] would give me the fifth character of the first string, but it's not. I get a "segmentation fault" which I believe is indicating that rec[0][4] doesn't exist. Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I've got an array of strings, rec[2], which contains four strings
    Actually, if it's declared as rec[2], you've only got two strings: rec[0] and rec[1].

    A segmentation fault is when you try to refer to a location in memory that doesn't belong to your program. So if you go out of bounds on an array/string, that's what happens.

    I suspect you're declaring the strings wrong and making assumptions about the outcome - can you post your entire code?

    One problem I notice is in your printf, you're using %s and supplying what you think is a char. %s expects a pointer to char, and prints the whole string of characters until a null-character is reached. If you want to print a single character, use %c.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You are trying to print a character, yet you used the %s format specifier instead of %c.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    Quote Originally Posted by bithub View Post
    You are trying to print a character, yet you used the %s format specifier instead of %c.
    Ah, that did it. Thanks! Like I said, I'm new to C and didn't realize there was a difference between %s and %c. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of strings?
    By mc72 in forum C Programming
    Replies: 5
    Last Post: 11-16-2008, 12:15 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. remove strings from array
    By ipe in forum C Programming
    Replies: 2
    Last Post: 01-12-2003, 04:53 AM
  5. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM