strcmp problem in c

This is a discussion on strcmp problem in c within the C Programming forums, part of the General Programming Boards category; I have two char array variables: char* key set to "foo" and char* test set to "bar" When I attempt ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05:24 PM
  2. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  3. Replies: 5
    Last Post: 11-07-2005, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21