Thread: Beginner help about strings

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    ISTANBUL
    Posts
    8

    Question Beginner help about strings

    Hi,

    I created this code:

    --------------------------------------------------
    #include<string.h>

    char a1[ ]="Available";


    //and when a condition is provided, I want to change value of a1 to "N/A"
    --------------------------------------------------

    How can I do this? Thanks...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a pointer, and just assign it the constant "N/A". Or, use an array that actually has a size, and copy the elements that you need.


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

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    ISTANBUL
    Posts
    8
    Thanks...

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    you can use only
    Code:
        char a1[ ]="Available";
    if you are not use any functions from string.h like strcpy

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by c.user View Post
    you can use only
    Code:
        char a1[ ]="Available";
    if you are not use any functions from string.h like strcpy
    Even considering your english is not perfect (that is OK with me), this is NONSENSE. Not true.

    You (c.user) should for your own good explain why you believe this. Look:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	char a1[]="Available";
    	strcpy(a1,"NONSENSE");
    	printf("%s\n",a1);	
    	return 0;
    }
    Last edited by MK27; 04-27-2009 at 07:55 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    you may compile this, there are no errors

    Code:
    main()
    {
        char a[] = "Available";
        return 0;
    }
    Code:
    #include <stdio.h>
    
    /* change value of array to another */
    main()
    {
        char a[] = "Available";
        const char *n = "N/A";
        unsigned len;
    
        sprintf(a, "%.*s", (len = sizeof a) > 0 ? len-1 : 0, n);
        printf("%s\n", a);
        return 0;
    }
    where is string.h ?
    oh, the program doesn't need it

    update:
    '\0' will be added
    Last edited by c.user; 04-27-2009 at 09:28 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by c.user View Post
    where is string.h ?
    oh, the program don't need it
    Not all string functions are in the string header.


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

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by c.user View Post
    you may compile this, there are no errors
    This is a fine option so I won't hold you up to a firing squad, but it still does not make sense of what you said before, which was that "you can only use [this] if you are not using string.h".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    MK27,
    Quote Originally Posted by my
    any functions from string.h
    if he does not use any function from string.h, he does not need string.h in the program

    string - is a chars + '\0'
    string function - is a function for string which has '\0'
    Last edited by c.user; 04-27-2009 at 09:39 PM.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by c.user View Post
    string function - is a function for string which has '\0'
    Any time you use an incomplete array, and declare it with an assignment from a string in quotes, it automatically has room for the null character.

    Because you've just used a string in quotes...

    Therefore, assuming you don't run off the end of the array's size there, most string functions will work just fine.

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

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Why not just create two char arrays?
    Code:
    char a1[] = "Available";
    char a2[] = "NotAvailable";
    And have your condition just call the one thats needed?
    And if your worried about the space just have one
    Code:
    char a2[] = "NotAvailable";
    If you need to print Available
    just start printing the char array from
    Code:
    a2[3] until '\0'
    If your just wanting to change the contents of the array that contains Available

    Just do
    Code:
    if(condition)
    {
    a1[0] = 'N';
    a1[1] = '/';
    a1[3] = 'A';
    a1[4] = '\0';
    }
    The rest of the array wont matter.
    But you cant stuff more stuff into the array without deleteing what was already there unless you made the array large enough to hold both to start with.
    Last edited by strictlyC; 04-28-2009 at 03:11 PM.

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by strictlyC View Post
    Why not just create two char arrays?
    Code:
    char a1[] = "Available";
    char a2[] = "NotAvailable";
    And have your condition just call the one thats needed?
    Or...
    Code:
    printf( "%sAvailable", available ? "" : "Not " );
    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by quzah
    Any time you use an incomplete array, and declare it with an assignment from a string in quotes, it automatically has room for the null character.
    If array is complete
    Code:
        char a[5] = "abcde";
    there will no '\0' in it

    and strcpy will continue copying while it will not get a '\0'

    and
    Code:
    #include <string.h>
    ...
        char a_mess[] = "Available";
        
        strcpy(a_mess, a);
    can cause segfault because a[5] may be a trash (not '\0')

    (a[5] like a sixth element of array a)
    Last edited by c.user; 04-28-2009 at 03:46 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by c.user View Post
    If array is complete
    It wasn't. That was the whole point.
    Quote Originally Posted by c.user View Post
    and strcpy will continue copying while it will not get a '\0'
    Which has nothing to do with the original post or the associated comment. No one was saying "Hey, if you use a malformed array as a string and try to use string functions over top of this array, it's not going to work!"


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

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by c.user View Post
    If array is complete
    Code:
        char a[5] = "abcde";
    there will no '\0' in it
    I dont think anyone said anything about initializing the array like that, not even the original poster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. beginner question about strings
    By gp364481 in forum C Programming
    Replies: 4
    Last Post: 09-05-2008, 06:31 PM
  3. beginner: dynamic array of strings
    By pc2-brazil in forum C++ Programming
    Replies: 10
    Last Post: 04-29-2008, 04:29 PM
  4. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  5. A beginner passing strings
    By jamjar in forum C Programming
    Replies: 2
    Last Post: 09-01-2002, 12:50 AM

Tags for this Thread