Thread: Basic question on character array

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Basic question on character array

    hi there,

    Is there any difference between these two kind of storage of characters in C

    Code:
    char array_ca[10]={"YES"};
                   
    OR
    
    char array_ca[0]='Y';
    char array_ca[1]='E';
    char array_ca[2]='S';
    Any help is appreciated!!

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Code:
    char array_ca[10]={"YES"};
    This is perfectly alright.
    Code:
    char array_ca[0]='Y';
    char array_ca[1]='E';
    char array_ca[2]='S';
    This is an error. You're redefining the array_ca again and again.
    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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    What in case I have an array name different for the second one I mean say,

    Code:
    char arr1_ca[0]='Y';
    char arr1_ca[1]='E';
    char arr1_ca[2]='S';
    However the first remains the same thats

    Code:
    char array_ca[10]={"YES"};
    Just wanted to know if these both character arrays are internally considered by C complier as same or they stored differently?

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Again you're redefining the arr1_ca multiple times. Remove the char keyword.
    But I guess I got what you want to ask.
    Code:
    char array_ca[10]={"YES"};
    In this case the char array gets automatically terminated by the '\0' chararacter.
    In the latter case you'll have to do it yourself like
    Code:
    char arr1_ca[4]; // only one definition
    arr1_ca[0]='Y';
    arr1_ca[1]='E';
    arr1_ca[2]='S';
    arr1_ca[3]='\0';
    Notice here I haven't redefined arr1_ca. It has got only one definition.
    If you would not do the null termination yourself, you'll get garbage printed out after the last character which will not happen in the former case.
    I hope I explained it well.
    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

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Thanks Ben for your reply. Yes I got what you said !

    But have certain things still on my mind still. Sorry abt that though!

    So if a do a string comparision on both of these character arrays, it would be a 0 using strncmp function for first 3 characters. Right? {Considering fact arr1_ca is null terminated explicitly}

    Also, in
    Code:
    array_ca[10]={"YES"};
    what would be value @array_ca[5] or [6] or for offsets where no value is stored ?

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by vandrea View Post
    Thanks Ben for your reply. Yes I got what you said !

    But have certain things still on my mind still. Sorry abt that though!

    So if a do a string comparision on both of these character arrays, it would be a 0 using strncmp function for first 3 characters. Right? {Considering fact arr1_ca is null terminated explicitly}

    Also, in
    Code:
    array_ca[10]={"YES"};
    what would be value @array_ca[5] or [6] or for offsets where no value is stored ?
    No need to say sorry for asking questions. People learn by asking questions and assimilating new information.
    Coming back to your question, using strcmp on two similar arrays will definitely result in 0.
    Secondly the array_ca is stored internally as
    Code:
    array_ca[0]='Y';
    array_ca[1]='E';
    array_ca[2]='S';
    array_ca[3]='\0';
    array_ca[4]='\0';
    array_ca[5]='\0';
    array_ca[6]='\0';
    array_ca[7]='\0';
    array_ca[8]='\0';
    array_ca[9]='\0';
    So, everything beyond the first NULL terminator gets filled with NULL terminators uptil the last char. You can check this by doing
    Code:
    printf("%c",array_ca[5]);
    It'll print a space which the ASCII representation of '\0', or by doing a %d it'll print 0 which the ASCII value of '\0'.
    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

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    Thanks a lot Ben that made things clear

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Yet another array question...
    By Raigne in forum C++ Programming
    Replies: 11
    Last Post: 11-13-2008, 01:55 PM
  2. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  3. pointer/ array question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 10-14-2008, 05:06 PM
  4. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 05:48 AM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM