Thread: Beginner help about strings

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    34
    You're probably better off using just a character pointer.
    Code:
    char *a1 = "Available";
    printf("%s\n",a1);
    if(1){
        a1 = "N/A";
    }
    printf("%s\n",a1);

  2. #17
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by mrcheesypants View Post
    You're probably better off using just a character pointer.
    Code:
    char *a1 = "Available";
    printf("%s\n",a1);
    if(1){
        a1 = "N/A";
    }
    printf("%s\n",a1);
    And now we're back to the very first response to this thread.

  3. #18
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    strickyc, if he is copying strings by functions which wait a '\0', he must know: 1) he may catch a problem with an array size (segfault) 2) he may catch a trouble with a function if it will not get a '\0' (trash in effect string or segfault)

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    None of that has anything to do with using incomplete character arrays and your statement that he can't use string.h functions with them. You were wrong, we aren't. That pretty much sums up everything in this thread beyond your first post.


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

  5. #20
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by c.user View Post
    strickyc, if he is copying strings by functions which wait a '\0', he must know: 1) he may catch a problem with an array size (segfault) 2) he may catch a trouble with a function if it will not get a '\0' (trash in effect string or segfault)
    The code you did as an example probaly won't compile in any good compiler so it was total rubbish

    char a[5] = "ABCDE";

    is junk. incorrect, and a very bad example.
    does that even compile with your compiler?
    If it does tell me which one.
    Last edited by strickyc; 04-28-2009 at 06:30 PM.

  6. #21
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by quzah
    your statement that he can't use string.h functions with them.
    where did I say it ? show me

    I said if he does not use any functions from string.h he may delete this include from the code

    Quote Originally Posted by strickyc
    char a[5] = "ABCDE";

    is junk. incorrect, and a very bad example.
    does that even compile with your compiler?
    it is correct code
    go and check, because it is correct initialization of a char array and its compiling is OK with a -Wall and gives only unused variable ‘a’
    Last edited by c.user; 04-29-2009 at 12:29 AM.

  7. #22
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    First off let me apologize to the OP for hi-jacking his/her thread.


    Quote Originally Posted by c.user View Post




    it is correct code
    go and check, because it is correct initialization of a char array and its compiling is OK with a -Wall and gives only unused variable ‘a’
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char a1[5] = "ABCDE";
    
       return 0;
    }
    error line 5: initializer-string for array of chars is too long

    Like I said name the compiler that it compiles on.(the one you used)
    so I know which one to avoid.

    Edit: I think your think that a[5] has six elements to it bu it doesn't. it ony has 5 elements.
    they are a[0],a[1],a[2],a[3],a[4] they is no actual a[5] in a[] a5 would be a[5-1] which is a[4] which is the fifth element of the array which in a array of characters the last element is reserved for the '\0' char. You can later over write the '\0' but IMO would be somewhat foolish if you plan on using the standard library to manipulate the char array.


    Maybe your compiler allows this I don't know, But I would think it's not a good compiler if it does.
    Can you name the compiler or what IDE it came with?
    Last edited by strickyc; 04-29-2009 at 01:52 AM.

  8. #23
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by strickyc View Post
    First off let me apologize to the OP for hi-jacking his/her thread. My last post in it.




    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char a1[5] = "ABCDE";
    
       return 0;
    }
    yes a1 has 5 characters in it with NULL being the last one.
    error line 5: initializer-string for array of chars is too long

    Like I said name the compiler that it compiles on.(the one you used)
    so I know which one to avoid.
    i copy pasted ur code above and it compiles with 0 errors and 0 warnings. But looking at ur error it looks right coz a1 has 5 characters in it. What is the problem which is causing this?
    Last edited by BEN10; 04-29-2009 at 02:12 AM.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  9. #24
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    i just compiled this piece of code and it gave me the error
    too many initializers
    Code:
    #include <stdio.h>
    #include<conio.h>
    int main(void)
    {
    	int a[3]={1,2,3,4};
       return 0;
    }
    plz someone tell me why is this error not shown in char arrays?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  10. #25
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by strickyc View Post
    First off let me apologize to the OP for hi-jacking his/her thread.


    Can you name the compiler or what IDE it came with?
    its in my signature. visual studio 2005. and i dont think its a bad compiler at all.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  11. #26
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    Quote Originally Posted by BEN10 View Post
    its in my signature. visual studio 2005. and i dont think its a bad compiler at all.
    I havent used visual studios well enough to know it. maybe they just consider it a logical error so left it out. they is no reason to say a[6] = "ABCDE" when a[ ] = "ABCDE" is much clearer.

    This would work with my compiler I think. But I knew user.c example wouldn't, Didnt even have to test it to see. But I did it to prove it.

    Code:
    char a[5] = {'A', 'B', 'C', 'D', 'E'};

    so does your complier compile

    char a1[3] = "reallylotsofstuff"
    Last edited by strickyc; 04-29-2009 at 02:18 AM.

  12. #27
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by strickyc View Post
    I havent used visual studios well enough to know it. maybe they just consider it a logical error so left it out.

    so does your complier compile

    char a1[3] = "reallylotsofstuff"
    yes this time also my compiler compiles it successfully but with a warning
    Code:
    Warning	1	warning C4045: 'a1' : array bounds overflow
    also if i try to access any element after the array bound it gives me run time error.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #28
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    ok, now i'm getting what's going on. If i try to run this code
    Code:
    char a1[5] = "ABCDE";
    it runs without any errors and warnings.
    But if i run this code
    Code:
    char a1[5] = "ABCDEF";
    it gives me warning : array bounds overflow
    in case of int array
    Code:
    int a1[5] = {1,2,3,4,5,6};
    the compiler gives me error: too many initializers
    can someone tell me why is this all happening? i'm confused by the behaviour of the C language.in the very first case, is the compiler ignoring the NULL character?
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, it's ignoring the NULL. The reason you're getting an error or warning is possibly because it's compiling as C++. It's illegal in C++. For some reason, it lets you get away with this when using character arrays, even though for all other data types it's an error to have more initializers than you have elements.

    Question 11.22


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

  15. #30
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by strickyc
    I think your think that a[5] has six elements
    no, I don't think that, it is from a book K&R

    Quote Originally Posted by strickyc
    But I would think it's not a good compiler if it does.
    )
    it is a good compiler (all compilers conform to standards of the language, if it doesn't, it is not a compiler of a language, because it is a compiler of a half-language )

    Quote Originally Posted by strickyc
    but IMO would be somewhat foolish if you plan on using the standard library to manipulate the char array.
    why do you think so ?
    I can use any function to manipulate the char array if it does all right (I can create my own function and use it in all program)

    Quote Originally Posted by BEN10
    can someone tell me why is this all happening?
    when you initialize an array of 5 elements by 6 elements it does a mistake

    "ABCDE" has 6 elements (we don't see a null-character)
    but initialization a[5] = "ABCDE" puts 5 elements in the array and '\0' doesn't put anywhere
    and initialization a[5] = "ABC" puts 4 elements in the array A, B, C, '\0'
    it is rule for the initialization only of char arrays

    and initialization a[] = "ABCDE" creates an array for all 6 elements (from a[0] to a[5])

    Quote Originally Posted by strickyc
    Code:
        char a[5] = {'A', 'B', 'C', 'D', 'E'};
    it's ok (no null-character)

    quzah says right, because in C99 they didn't cancel this rule for initialization (only in C++ it can cause an error)

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