Thread: problem with strings

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    problem with strings

    i'm trying to make this the variable 'blanks' filled with underscores so that both the length of the 'blanks' and the word are the same. but the compiler tells me that the assignment on the fourth line "makes an integer from pointer without cast". why does it return an integer in the first place?

    Code:
    theWord = "hello"
    theLength = strlen(theWord);
    char blanks[theLength];
    for (i=0;i<theLength;i++) blanks[i] = "_";

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    blanks[i] = '_';
    Double quotes are string literals.
    Single quotes are character literals.

    Code:
    char blanks[theLength];
    Just know that that is a C99 extension.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Also, you should be using strcpy to assign "hello" to theWord if it isn't a declaration and initialization and don't forget you'll have to set blanks[theLength-1] equal to '\0' otherwise it's not technically a string.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    slymaelstrom: thanks for the reminder about the '\0', it was displaying funny characters after my string until i put that in.

    just so i know in the future, what's the difference between double quotes and single quotes?

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by echo49
    just so i know in the future, what's the difference between double quotes and single quotes?
    A null.

    Code:
    'a'     is     'a'
    "a"     is     'a' + '\0'
    Sent from my iPadŽ

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    just so i know in the future, what's the difference between double quotes and single quotes?
    A string literal returns the adress of the string whereas a character literal returns the character.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    To be pedantic, neither one of them return anything.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 08:01 PM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 10:44 PM
  3. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  4. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  5. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM