CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: empty char

  1. #1
    Join Date
    May 2018
    Posts
    158

    empty char

    An exercise asks to initialize a matrix because afterwards It's necessary to insert words (char by char), so how can I set empty char?
    I thought to set in this way:

    Code:
    char word='\n'
    Newline is not the same thing of empty char! If I print this char I got newline but empty char should print nothing.

    but I'm trying to understand if there is way to define char as empty, just for example as for string I use:

    Code:
    char tree[]=""

  2. #2
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: empty char

    Use \0.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: empty char

    But what exactly is your code?
    Code:
    char word='\n'
    is not a word, but a letter.

    What exactly are you trying?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    May 2018
    Posts
    158

    Re: empty char

    Word is only a identifier and the name is wrong.
    I'd like to reset matrix before to write it some characters, so I need to write on each element null value.
    So It's correct to write this one ?

    Code:
     char r='\0'

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: empty char

    How is the matrix defined? There are probably ways of initializing it without setting each value.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    May 2018
    Posts
    158

    Re: empty char

    Quote Originally Posted by 2kaud View Post
    How is the matrix defined?
    matrix is defined as NxN according to:

    Code:
    char** matrix;
    matrix=new char*[N];
    for (int i=0; i<N; i++)
       matrix[i]=new char[N];

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: empty char

    To create and initialise each element to its default init value (\0 for a char), consider

    Code:
    	auto matrix = new char*[N];
    
    	for (int i = 0; i < N; ++i)
    		matrix[i] = new char[N] {};
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured