Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9279639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:41:01+00:00 2026-06-18T17:41:01+00:00

A few things. When I declare my struct with a member array of size

  • 0

A few things.

  1. When I declare my struct with a member array of size 3, wouldn’t that mean that array has 4 elements? 0, 1, 2, 3? Why them, when I try to insert the characters A, B, and C, it tells me initializer-string for array of chars is too long [-fpermissive]?

    #include <iostream>
    using namespace std;
    
    struct Student {
        double no;
        char grade[3];
    };
    
    int main() {
        struct Student harry = {975, "ABC"};
    }
    
  2. When I print the address of a specific index of a character array I get the following results from the following code:

    struct Student {
        double no;
        char grade[4];
    };
    
    int main() {
    
        struct Student harry = {975, "ABC"};
    
        for (int i = 0; i < 4; i++)
            cout << "h.g[" << i << "]" << harry.grade[i] << endl;
    
        for (int i = 0; i < 4; i++)
            cout << "h.g[" << i << "]" << &harry.grade[i] << endl;
    }
    

Results:

h.g[0]A
h.g[1]B
h.g[2]C
h.g[3]
&h.g[0]ABC
&h.g[1]BC
&h.g[2]C
&h.g[3]

Why does the first index print ABC, and then BC, and so forth instead of each character separately like the first loop?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:41:02+00:00Added an answer on June 18, 2026 at 5:41 pm

    No, declaring an array like T arr[3] gives you an array with 3 elements, not 4. The 3 in the declaration is the size of the array. Indices start at 0, so the indices for the elements are 0, 1, and 2.

    The string literal "ABC" gives you an “array of 4 const char” where the last element is the null character. Your program is ill-formed if you attempt to initialise an array with a string literal that has too many characters:

    There shall not be more initializers than there are array elements.

    In the first loop you are getting each character of the array and printing it out. When you print a char you get only that char as output.

    When you take the address of an element of the array, with &harry.grade[i], you get a char*. When you output a char*, the I/O library treats it as a C-style null-terminated string. It will output from that character to the first null character it finds. That’s why you get the character at position i and the characters following it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are a few things that the custom grid does that you cannot do
i have a header file global.h where i declare a few variables that i
I'm trying to write a stored procedure in which I insert a few things
There are a few things that I almost always do when I put a
I'm rebuilding Josh Smith's WPF CommandSink example and there are a few things that
I'm implementing a few things in Ruby and I was wondering how much error
I can do a few things with PowerPoint, but I need to learn more
I am trying out few things on windows with emacs. In my case, I
I am experimenting with a few things for a new UI in one of
I am trying to separate a few things in here. I have a program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.