Thread: strcmp problem in c

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    22

    strcmp problem in c

    I have two char array variables:

    char* key set to "foo"

    and char* test set to "bar"

    When I attempt a strcmp(key, test), my c program seg faults.

    What's going on here?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Prodiga1 View Post
    I have two char array variables:

    char* key set to "foo"

    and char* test set to "bar"

    When I attempt a strcmp(key, test), my c program seg faults.

    What's going on here?
    Perhaps we could be of more help if you posted the actual code....

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    My guess is you didn't actually do

    char *key = "foo";

    But instead you either
    - forgot to allocate any space for your pointers
    - forgot to append a \0 when constructing your strings, one character at a time.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM