Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. array values

array values

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelptutorialquestion
15 Posts 6 Posters 0 Views 1 Watching
  • 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.
  • P Pryabu

    Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,

    C Offline
    C Offline
    Code o mat
    wrote on last edited by
    #2

    Well, there can be multiple answers to that, depending on your needs, what tools you have available, what environment you work in e.g:

    char sz123\[10\]\[20\];
    char c\[20\] = "test";
    strcpy(sz123\[0\], c);
    strcpy(c, "123");
    

    or

    std::string sz123\[10\];
    char c\[20\] = "test";
    sz123\[0\] = c;
    strcpy(c, "123");
    

    or

    CString sz123\[10\];
    char c\[20\] = "test";
    sz123\[0\] = c;
    strcpy(c, "123");
    

    or

    LPSTR sz123\[10\];
    char c\[20\] = "test";
    sz123\[0\] = (LPSTR)malloc(sizeof(c\[0\]) \* (strlen(c) + 1));
    strcpy(sz123\[0\], c);
    strcpy(c, "123");
    ...
    free(sz123\[0\]);
    

    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > "It doesn't work, fix it" does not qualify as a bug report. < > Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <

    1 Reply Last reply
    0
    • P Pryabu

      Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #3

      you declared array of char pointers sz123 and stored the base adrress of string c to its 0th index. So whenever value in c changes, its reflected in sz123. If you want to store each strings in sz123, allocate memory for each.

      LPSTR sz123[10];

      char c[20] = "test";

      sz123[0] = new char[strlen(c)+1];
      strcpy(sz123[0], c);

      strcpy(c,"123"); //wont affect sz123[0]

      1 Reply Last reply
      0
      • P Pryabu

        Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #4

        Remember you're not really copying values around, you're copying pointers. So when you do:

        sz123[ 0 ] = c;

        You're just creating an alias to the block of memory that contains "hello." I'd suggest (unless you're stuck programming in C) to avoid using character arrays, character pointers and C style strings and just use C++ strings - then when you copy them around you're actually copying values and not aliases. Cheers, Ash

        1 Reply Last reply
        0
        • P Pryabu

          Hi, In the following code,i have assigned a value "test" to c variable. I have assigned the variable c to sz123. If i copied another value to c,the already assigend value in sz123[0] is getting overide. Can anyone please help me how to avoid this one? LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)c; strcpy(c,"123"); thanks,

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #5

          sz123[0] = _strdup(c);
          //...
          free(sz123[0]);

          :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          P 1 Reply Last reply
          0
          • CPalliniC CPallini

            sz123[0] = _strdup(c);
            //...
            free(sz123[0]);

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            P Offline
            P Offline
            Pryabu
            wrote on last edited by
            #6

            Is it possible to use the globally declared LPSTR variable in other class by using extern function?

            CPalliniC 1 Reply Last reply
            0
            • P Pryabu

              Is it possible to use the globally declared LPSTR variable in other class by using extern function?

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #7

              Karthika85 wrote:

              Is it possible to use the globally declared LPSTR variable in other class by using extern function?

              You're mixing a bit different domains here. If the variable is global then you may use it everywhere (don't forget to call free when you no longer need the string). :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              P 2 Replies Last reply
              0
              • CPalliniC CPallini

                Karthika85 wrote:

                Is it possible to use the globally declared LPSTR variable in other class by using extern function?

                You're mixing a bit different domains here. If the variable is global then you may use it everywhere (don't forget to call free when you no longer need the string). :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                P Offline
                P Offline
                Pryabu
                wrote on last edited by
                #8

                No,I am not able to use the globally declared sz123[0] variable in another class: In class1: LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)malloc(sizeof(c[0]) * (strlen(c) + 1)); strcpy(sz123[0], c); In class2: extern LPSTR sz123[10]; char ch1[20]; strcpy(ch1,sz123[0]); It is showing the following error "error LNK2001: unresolved external symbol "char * * sz123":

                CPalliniC L 2 Replies Last reply
                0
                • CPalliniC CPallini

                  Karthika85 wrote:

                  Is it possible to use the globally declared LPSTR variable in other class by using extern function?

                  You're mixing a bit different domains here. If the variable is global then you may use it everywhere (don't forget to call free when you no longer need the string). :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  P Offline
                  P Offline
                  Pryabu
                  wrote on last edited by
                  #9

                  Got it.I should not use static keyword to use in other cpp files

                  CPalliniC 1 Reply Last reply
                  0
                  • P Pryabu

                    No,I am not able to use the globally declared sz123[0] variable in another class: In class1: LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)malloc(sizeof(c[0]) * (strlen(c) + 1)); strcpy(sz123[0], c); In class2: extern LPSTR sz123[10]; char ch1[20]; strcpy(ch1,sz123[0]); It is showing the following error "error LNK2001: unresolved external symbol "char * * sz123":

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #10

                    You must be precise. A global variable cannot be declared inside a class (it wouldn't be global). So what do you intend to do? Do you want to use a global variable? Do you want to make an object's member variable available to objects of different classes? :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    P 1 Reply Last reply
                    0
                    • P Pryabu

                      Got it.I should not use static keyword to use in other cpp files

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #11

                      Of course. The static qualifier, for varibles declared outside classes, makes the variables themselves having file-scope. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • CPalliniC CPallini

                        You must be precise. A global variable cannot be declared inside a class (it wouldn't be global). So what do you intend to do? Do you want to use a global variable? Do you want to make an object's member variable available to objects of different classes? :)

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                        [My articles]

                        P Offline
                        P Offline
                        Pryabu
                        wrote on last edited by
                        #12

                        I decalred a global variable array not class variable in one class and i want to use that gloabl variable in another class.

                        CPalliniC 1 Reply Last reply
                        0
                        • P Pryabu

                          I decalred a global variable array not class variable in one class and i want to use that gloabl variable in another class.

                          CPalliniC Offline
                          CPalliniC Offline
                          CPallini
                          wrote on last edited by
                          #13

                          And what's your problem? For instance:

                          // global.cpp
                          int global_counter=0;

                          and

                          // source.cpp
                          extern int global_counter;

                          void show_counter()
                          {
                          cout << global_counter << endl;
                          }

                          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                          [My articles]

                          In testa che avete, signor di Ceprano?

                          P 1 Reply Last reply
                          0
                          • CPalliniC CPallini

                            And what's your problem? For instance:

                            // global.cpp
                            int global_counter=0;

                            and

                            // source.cpp
                            extern int global_counter;

                            void show_counter()
                            {
                            cout << global_counter << endl;
                            }

                            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                            [My articles]

                            P Offline
                            P Offline
                            Pryabu
                            wrote on last edited by
                            #14

                            previously i used static keyword. so,i got that error. now i removed the keyword static.

                            1 Reply Last reply
                            0
                            • P Pryabu

                              No,I am not able to use the globally declared sz123[0] variable in another class: In class1: LPSTR sz123[10]; char c[20] = "test"; sz123[0] = (LPSTR)malloc(sizeof(c[0]) * (strlen(c) + 1)); strcpy(sz123[0], c); In class2: extern LPSTR sz123[10]; char ch1[20]; strcpy(ch1,sz123[0]); It is showing the following error "error LNK2001: unresolved external symbol "char * * sz123":

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #15

                              You need to declare it extern in all modules, and initialize it in only one.

                              It's time for a new signature.

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups