0

So I've declared a 2d array and want to display its rows one by one.But when I execute the following code I see all the rows in the matrix starting from the one I require.

Code:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

char topics[3][10]={"Literature","Science---","Sports----"};

void main()
{
    clrscr();
    cout<<topics[0][0]<<endl<<topics[1][0]<<endl<<topics[2][0]<<endl;
    puts(topics[0]);
    cout<<endl;
    puts(topics[1]);
    getch();
}

Output:

L    
S   
S   
LiteratureScience---Sports----         
Science---Sports----

What I want my program to do is that when puts(0) is executed, only 'Literature' should be displayed and when puts(1) is executed, only 'Science---' is displayed.

I am a beginner. Please suggest what corrections should I make.Thank you. :)

Pbd
  • 1,219
  • 1
  • 15
  • 32
stardust
  • 1
  • 1

3 Answers3

3

The declaration:

char topics[3][10]={"Literature","Science---","Sports----"};

Is taken by the compiler as:

char topics[3][10]={"Literature\0","Science---\0","Sports----\0"};

Which means that each one of those strings consists of 11 characters.

Therefore you need to change topics[3][10] to topics[3][11].

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • And BTW, you've got an extra `-` in the last string, which makes consisting of even 12 characters. – barak manos Feb 12 '17 at 05:16
  • "Sports" is 6 characters - he's added four hyphens as a result to pad it to 10. There's no error there. – Caleb Feb 12 '17 at 05:56
  • @Caleb: Ooops... that's right, thanks. BTW, your edit attempt - it is better as a comment. The answer is slightly different for C and for C++ (yours was appropriate for C if I remember correctly). – barak manos Feb 12 '17 at 06:01
1

The problem is that strings in C are terminated by \0. Thus you need 11 chars to store a string of length 10. This last char is simply skipped if you make your array too small (see Why "initializer-string for array of chars is too long" compiles fine in C & not in C++?). Therefore puts does not find a \0 at the end of the string and instead goes to the end of the array because (luckily) the memory after the array contains a zero. As commented this can be fixed making the array the correct size

#include <stdio.h>

char topics[3][11]={"Literature","Science---","Sports----"};

int main()
{
    puts(topics[0]);
    puts(topics[1]);
}

The output is

Literature
Science---

A C++ compiler would not accept your program:

3:59: error: initializer-string for array of chars is too long [-fpermissive]
3:59: error: initializer-string for array of chars is too long [-fpermissive]
3:59: error: initializer-string for array of chars is too long [-fpermissive]

go away.

Community
  • 1
  • 1
  • I believe that the compiler adds a `\0` to every string literal, so it's not about `puts` not finding the null-character. – barak manos Feb 12 '17 at 05:18
  • Yes you are correct. But since the initializer is too long this trailing `\0` is not actually stored. Thus the next string starts immediately without an `\0` inbetween to tell `puts` that the string has ended. – Jonathan von Schroeder Feb 12 '17 at 05:37
  • OK, I would tend to guess (though I'm not sure, since I don't have every possible platform at hand), that some compilers would give a compilation error for attempting to initialize an array of an explicit size with an amount of elements larger than that size. – barak manos Feb 12 '17 at 05:42
  • In C it's just a warning. In C++ it is not allowed http://stackoverflow.com/questions/28433862/why-initializer-string-for-array-of-chars-is-too-long-compiles-fine-in-c-not – Jonathan von Schroeder Feb 12 '17 at 05:45
1

You wouldn't need to mention that array's dimensions explicity. The compiler does the job.

const char *topics[] = {"Literature", "Science---", "Sports----"};

int main() {
    puts(topics[0]);
    puts(topics[1]);
}

Note that in this way, those strings are read-only.

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Correct, though that would change the nature of this array from an array of modifiable strings to an array of non-modifiable strings. If OP ever tries to change any of those strings, then he/she will run into yet another problem. – barak manos Feb 12 '17 at 05:19
  • @barakmanos Yes, you're right. Using my code strings are read-only. – frogatto Feb 12 '17 at 05:24
  • So you might want to mention that as part of your answer (e.g., "If you're not planning to change any of those strings during the execution of your program, then you can..."). – barak manos Feb 12 '17 at 05:25