Why is this wrong?

This is a discussion on Why is this wrong? within the C Programming forums, part of the General Programming Boards category; I saw this on a tutorial, but it doesnt work: Code: char multi[5][10]; multi[0] = {'0','1','2','3','4','5','6','7','8','9','0'};...

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    Why is this wrong?

    I saw this on a tutorial, but it doesnt work:


    Code:
     char multi[5][10];
          multi[0] = {'0','1','2','3','4','5','6','7','8','9','0'};

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    Code:
    {'0','1','2','3','4','5','6','7','8','9','0'}
    Initializes an array, but you are actually defining it first and then assigning, which is a no-no.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    6
    You can do that only in defining line.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Its also initializing 11 array members, but the array is only 10.

  5. #5
    Banned
    Join Date
    Mar 2008
    Posts
    78
    I didnt understand well, can you type an example for me?

    I want to make a 2D matrix (array of arrays), so i saw on a tutorial this way:

    Code:
     char multi[5][10];
          multi[0] = {'0','1','2','3','4','5','6','7','8','9'};
          multi[1] = {'0','1','2','3','4','5','6','7','8','9'};
          multi[2] = {'0','1','2','3','4','5','6','7','8','9'};
          multi[3] = {'0','1','2','3','4','5','6','7','8','9'};
          multi[4] = {'0','1','2','3','4','5','6','7','8','9'};
    But i get this error:

    syntax error before '{' token in line:
    "multi[0] = {'0','1','2','3','4','5','6','7','8','9'};"
    Last edited by Milhas; 03-26-2008 at 11:30 AM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    Code:
    char multi[] = {'0','1','2','3','4','5','6','7','8','9','0'};
    char multi[][2] = { { '0', '1' }, { '2', '3' } };
    And so on.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by Elysia View Post
    Code:
    char multi[] = {'0','1','2','3','4','5','6','7','8','9','0'};
    char multi[][2] = { { '0', '1' }, { '2', '3' } };
    And so on.
    I'm sorry, but that doesnt work either...

    Error:

    conflicting types for 'multi'

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,659
    Of course not. It was two examples, not one. It's how you do it. You copy how I did it with your own variables.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned
    Join Date
    Mar 2008
    Posts
    78
    ok, tnks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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