Help with strings

This is a discussion on Help with strings within the C Programming forums, part of the General Programming Boards category; I can't seem to figure out how to set a string array equal to a particular character string. For example: ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Help with strings

    I can't seem to figure out how to set a string array equal to a particular character string. For example:

    Code:
    char yourname[40];
    
    yourname = 'Will Ferrell';
    I know the above is not valid, but that is what I'm trying to do.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Check out the strcpy() function.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Or maybe you could do something with pointers!

    Code:
    #include <stdio.h>
    
    int main()
    {
        char *string;
        
        scanf("%s", &string);
        printf("%s", &string);
        
        return 0;
    }
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by xxxrugby
    Or maybe you could do something with pointers!

    Code:
    #include <stdio.h>
    
    int main()
    {
        char *string;
        
        scanf("%s", &string);
        printf("%s", &string);
        
        return 0;
    }
    OMFG No! You're joking right?! Please tell me you're joking!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Ups. I think that he wont something else. Sorry. I now take a dictionary and see that my translation was not wery good!
    I translate that he want a string of let say five char. And then the array is make by five places, after the string was insert!
    My mistake!
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    xxxrugby, your code gave me hives.
    If you understand what you're doing, you're not learning anything.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xxxrugby
    Or maybe you could do something with pointers!

    Code:
    #include <stdio.h>
    
    int main()
    {
        char *string;
        
        scanf("%s", &string);
        printf("%s", &string);
        
        return 0;
    }
    Since everyone likes a helpful reply, here's mine:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char *string;  /* 1 */ 
        
        scanf("%s", &string);  /* 2 */ 
        printf("%s", &string);  /* 3 */ 
        
        return 0;
    }
    1) Here you declare a pointer. Where is it pointing now? In other words, your pointer has never been set to point at anything, so it's pointing at some random spot in memory. It has no memory allocated for anything other than the pointer itself.

    2) Here you scan into where the pointer is pointing. Where is it pointing again? Oh, also, your scanf call is wrong. You don't need to give the address-of the pointer, because scanf expects a pointer, not the address of a pointer. So remove the & when you're using a pointer already.

    3) Your printf call is also wrong. Even if this weren't a pointer, you wouldn't use the address-of operator. You never use the address-of operator with printf, unless you are in fact trying to display the address-of a variable.

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

  8. #8
    Registered User xxxrugby's Avatar
    Join Date
    Jan 2005
    Posts
    178
    Yes you need in scanf and printf &
    If I remove them then I get
    Code:
    The instruction at "0x78...." referenced memory at "0x00....". This memory could not be "read"
    Sorry for spelling errors, not English!
    xxxrugby: "All Human Race Will Die From My Hand!"
    xxxrugby: "We are all philosophers, when question is about politics!"

  9. #9
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>Yes you need in scanf and printf &
    >>If I remove them then I get
    You missed the point. It's broken either way if the pointer doesn't point to anything except a random address.
    Kampai!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xxxrugby
    Yes you need in scanf and printf
    Please don't act like you know what you're talking about.

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

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by xxxrugby
    Yes you need in scanf and printf &
    If I remove them then I get
    This is likely because of the first point quzah made, i.e.:

    Quote Originally Posted by quzah
    1) Here you declare a pointer. Where is it pointing now? In other words, your pointer has never been set to point at anything, so it's pointing at some random spot in memory. It has no memory allocated for anything other than the pointer itself.
    You need to malloc some space for the string pointer to point to before you start saving data to that location.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Just to clear anything up, I'm not trying to get input from the user. I have the string that should be put in the array. I know you can't put a string into an array with a simple assisgnment statement. My question is then, how do you do it?

  13. #13
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Actually,
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	char myString[] = "I can do this";
    
    	printf("%s\n", myString);
    
    	return 0;
    }
    This is what is known as a string literal, and the compiler automatically assigns the terminating character \0 to the array.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by nizbit
    Just to clear anything up, I'm not trying to get input from the user. I have the string that should be put in the array. I know you can't put a string into an array with a simple assisgnment statement. My question is then, how do you do it?
    strcpy
    Last edited by Dave_Sinkula; 02-11-2005 at 09:22 PM. Reason: Added link.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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