Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Why my below statement throw error ?
Qt 6.11 is out! See what's new in the release blog

Why my below statement throw error ?

Scheduled Pinned Locked Moved Unsolved C++ Gurus
10 Posts 5 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #1

    I have question regarding below statement

    record[0].name[30] ="mangal";

    when i use it in below code it throw error. i want to know technical reason behind that .

    #include <stdio.h>
    #include <string.h>
    struct student{
        int id;
        char name[30];
        float percentage;
    };
    int main(){
        int i;
        struct student record[2];
        // 1st student's record
        record[0].id=1;
        record[0].name[30] ="mangal";
       // strcpy(record[0].name, "Bhanu");
    
    JonBJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      I have question regarding below statement

      record[0].name[30] ="mangal";

      when i use it in below code it throw error. i want to know technical reason behind that .

      #include <stdio.h>
      #include <string.h>
      struct student{
          int id;
          char name[30];
          float percentage;
      };
      int main(){
          int i;
          struct student record[2];
          // 1st student's record
          record[0].id=1;
          record[0].name[30] ="mangal";
         // strcpy(record[0].name, "Bhanu");
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Qt-embedded-developer said in Why my below statement throw error ?:

      when i use it in below code it throw error. i want to know technical reason behind that .

      Seriously? Do you think posting the error message you are receiving from the compiler would be relevant, or should we guess?

      record[0].name[30] ="mangal";

      The most basic C/C++. What type is record[0].name[30]? I'm quite sure whatever error message you are receiving tells you this....

      Q 1 Reply Last reply
      2
      • JonBJ JonB

        @Qt-embedded-developer said in Why my below statement throw error ?:

        when i use it in below code it throw error. i want to know technical reason behind that .

        Seriously? Do you think posting the error message you are receiving from the compiler would be relevant, or should we guess?

        record[0].name[30] ="mangal";

        The most basic C/C++. What type is record[0].name[30]? I'm quite sure whatever error message you are receiving tells you this....

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by
        #3

        @JonB error is telling me assignment is not possible. But i not know why ?

        jsulmJ L JonBJ KroMignonK 5 Replies Last reply
        0
        • Q Qt embedded developer

          @JonB error is telling me assignment is not possible. But i not know why ?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Qt-embedded-developer PLEASE post the whole error!
          And PLEASE also answer @JonB question!
          Help others to help you, do not make it unnecessary difficult to help you!

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Q Qt embedded developer

            @JonB error is telling me assignment is not possible. But i not know why ?

            L Offline
            L Offline
            lacuna
            wrote on last edited by lacuna
            #5

            @Qt-embedded-developer
            Your error might be this:
            error: array type 'char [30]' is not assignable

            You just can't do "record[0].name[30] ="mangal";"
            As the contents are modifiable, the arrays themselves are not.
            Use strcpy from <string.h> instead.

            #include <stdio.h>
            #include <string.h>
            #include <iostream>
            
            struct student{
                int id;
                char name[30];
                float percentage;
            };
            
            int main(){
                
                int i;
                
                struct student record[2];
                
                // 1st student's record
                record[0].id=1;
                strcpy(record[0].name,"mangal");
                
                // 2st student's record
                record[1].id=2;
                strcpy(record[1].name,"himanshu");
                
                //test
                std::cout << record[0].name << std::endl;
                std::cout << record[1].name << std::endl;
            }
            

            Just one hint - I would do the "record = id" ;-) its easier to handle.

            1 Reply Last reply
            4
            • Q Qt embedded developer

              @JonB error is telling me assignment is not possible. But i not know why ?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Qt-embedded-developer Why don't you use QString instead of char array?
              @lacuna explained you why you get that error (you need to understand that you can't assign to an array like you do).

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • Q Qt embedded developer

                @JonB error is telling me assignment is not possible. But i not know why ?

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Qt-embedded-developer said in Why my below statement throw error ?:

                @JonB error is telling me assignment is not possible. But i not know why ?

                For goodness sake, that is not an error message.... Really how do you expect people to help you?

                1 Reply Last reply
                0
                • Q Qt embedded developer

                  @JonB error is telling me assignment is not possible. But i not know why ?

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @Qt-embedded-developer said in Why my below statement throw error ?:

                  But i not know why ?

                  Are you understanding what you are coding?
                  You have defined a struct which constains an int value called id, a float value call percentage and an array of char called name which have 30 elements.

                  And in your code you are trying update item 31 of name and you are surprise that this do not work!!

                  Are you serious?

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  JonBJ 1 Reply Last reply
                  0
                  • KroMignonK KroMignon

                    @Qt-embedded-developer said in Why my below statement throw error ?:

                    But i not know why ?

                    Are you understanding what you are coding?
                    You have defined a struct which constains an int value called id, a float value call percentage and an array of char called name which have 30 elements.

                    And in your code you are trying update item 31 of name and you are surprise that this do not work!!

                    Are you serious?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9

                    @KroMignon said in Why my below statement throw error ?:

                    And in your code you are trying update item 31 of name and you are surprise that this do not work!!

                    It's not that which I was drawing attention to. I am expecting OP to get a compiler error message:

                    struct student{
                        char name[30];
                    }
                    record[0].name[30] ="mangal";
                    

                    The type of name[30] versus the type of "...".

                    KroMignonK 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @KroMignon said in Why my below statement throw error ?:

                      And in your code you are trying update item 31 of name and you are surprise that this do not work!!

                      It's not that which I was drawing attention to. I am expecting OP to get a compiler error message:

                      struct student{
                          char name[30];
                      }
                      record[0].name[30] ="mangal";
                      

                      The type of name[30] versus the type of "...".

                      KroMignonK Offline
                      KroMignonK Offline
                      KroMignon
                      wrote on last edited by KroMignon
                      #10

                      @JonB said in Why my below statement throw error ?:

                      It's not that which I was drawing attention to. I am expecting OP to get a compiler error message:

                      Yes, I also saw this, but this is one of many error in this code extract:

                      • index out of bounds
                      • bad types
                      • wrong assignment statement
                      • ...

                      Impressive how many errors can be done in less than 100 lines of code :D

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved