943,688 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1356
  • C++ RSS
Sep 16th, 2009
0

Use of pointers to assign values to a string array

Expand Post »
Two lines in the codes below have been remarked out else compiling would fail:

//arrcon[1]=*scon;
//count << "arrcon[1] " << arrcon[1];

The intention was to assign character values to an array with 5 elements, each with a maximum 35 characters, declared as a string array, arrcon[5],[35];

I was trying to use pointer, but there must be bug/s somewhere. It did not work. See codes below.

Kindly point out where I went wrong.

C++ Syntax (Toggle Plain Text)
  1. // pointers Use of pointers to assign values to a string array
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <string>
  6. #include <stdlib.h>
  7.  
  8. using namespace std;
  9. int main ()
  10. {
  11. string arrcon[5][35];
  12. string* scon;
  13. string name;
  14. string xcon;
  15. name="YBY";
  16. scon=&name;
  17. xcon=*scon;
  18.  
  19. cout << "\nname = " << name;
  20. cout << "\nscon= mem add. of name: " << scon;
  21. cout << "\nxcon= value of name: " << xcon << "\n\n";
  22.  
  23. // Now using array
  24.  
  25. //arrcon[1]=*scon;
  26. //count << "arrcon[1] " << arrcon[1];
  27.  
  28. // use of pointers courtesy of http://www.cplusplus.com/doc/tutorial/pointers/
  29. cout << "\n\nUse of pointers courtesy of http://www.cplusplus.com\n\n";
  30. int numbers[5];
  31. int * p;
  32. p = numbers; *p = 10;
  33. p++; *p = 20;
  34. p = &numbers[2]; *p = 30;
  35. p = numbers + 3; *p = 40;
  36. p = numbers; *(p+4) = 50;
  37.  
  38. for (int n=0; n<5; n++) {
  39. cout << "\t" << numbers[n] << ", ";
  40. }
  41.  
  42. cout << "\n\n";
  43. getch();
  44. return 0;
  45. }
Similar Threads
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009
Sep 16th, 2009
1

Re: Use of pointers to assign values to a string array

This:
string arrcon[5][35];
is basically a table of words/strings composed of 5 lines and 35 words/strings per line. The size of each word/string is indefinite. arrcon[1] is the second line of 35 words/strings, not a single word/string or a pointer to a string. scon is a pointer to a single string/word. *scon is the word/string that scon is pointing to.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Sep 16th, 2009
1

Re: Use of pointers to assign values to a string array

It is not that you are creating 5 strings of size 35, But actually Creating an 2-d array of 5 rows and 35 columns.

The size of the string is not constant and can extend as much as possible ,until you have the "space" to store such a long string.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Sep 16th, 2009
0

Re: Use of pointers to assign values to a string array

Click to Expand / Collapse  Quote originally posted by Lerner ...
This:
string arrcon[5][35];
is basically a table of words/strings composed of 5 lines and 35 words/strings per line. The size of each word/string is indefinite. arrcon[1] is the second line of 35 words/strings, not a single word/string or a pointer to a string. scon is a pointer to a single string/word. *scon is the word/string that scon is pointing to.
Dear Lerner,

My understanding of arrays is based on basic and foxpro, where you can assign values to arrays. Are there other C++ string functions which can be used for this purpose? Or is c-string array better for this purpose?
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009
Sep 16th, 2009
0

Re: Use of pointers to assign values to a string array

It is not that you are creating 5 strings of size 35, But actually Creating an 2-d array of 5 rows and 35 columns.

The size of the string is not constant and can extend as much as possible ,until you have the "space" to store such a long string.
I was trying to store character values inside an arrays, eg. arrcon[], which can be displayed using a for() loop.
say name="John", weight="56", store these variable values into arrcon[1], arrcon[2]. What are the C++ syntax statements to perform this assignment?
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009
Sep 16th, 2009
0

Re: Use of pointers to assign values to a string array

I'm not 100% sure what you are asking is this correct?

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  
  9. string arrcon[2];
  10.  
  11. arrcon[0] = "John";
  12. arrcon[1] = "56";
  13.  
  14. for( int i = 0; i < 2; i++ )
  15. {
  16. cout << arrcon[i] << endl;
  17. }
  18.  
  19. system("PAUSE");
  20. return 0;
  21. }
Reputation Points: 164
Solved Threads: 98
Practically a Master Poster
sfuo is offline Offline
642 posts
since Jul 2009
Sep 16th, 2009
1

Re: Use of pointers to assign values to a string array

You misspelt cout,
  //arrcon[1]=*scon;
 //count << "arrcon[1] " << arrcon[1];

string arrcon[5][35]; is a two-dimensional string array,
C++ Syntax (Toggle Plain Text)
  1. arrcon[0][0]="Hello";
  2. arrcon[0][1]=*scon;
  3. cout << "\n" << arrcon[0][0] << " " << arrcon[0][1];
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Sep 18th, 2009
0

Re: Use of pointers to assign values to a string array

Click to Expand / Collapse  Quote originally posted by adatapost ...
You misspelt cout,
[code]
string arrcon[5][35]; is a two-dimensional string array,
[code]
Dear Adatapost,

Thanks for your tips. i think i got. You need not specify length of each string array element. string arrcon[5][35] is a 2d string array of indefinite length.

The working code snippet below is supported by Borland, CODE::BLOCK and Dev-C++, for the latter two, #include<string> may be removed.

C++ Syntax (Toggle Plain Text)
  1. // pointers3 YBM Use of pointers to assign values to a string array
  2. #include <iostream>
  3. #include <conio.h>
  4. #include <string>
  5. using namespace std;
  6. int main () {
  7. string arrcon[5][35];
  8. string* scon;
  9. arrcon[0][0]="Hello";
  10. scon = &arrcon[0][0];
  11. arrcon[0][1]=*scon;
  12. cout << "\n" << arrcon[0][0] << " via pointer= " << arrcon[0][1];
  13. cout << "\n\n";
  14. getch();
  15. return 0;
  16. }
Reputation Points: 39
Solved Threads: 0
Junior Poster in Training
yonghc is offline Offline
55 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Read in/Write from Memorystream
Next Thread in C++ Forum Timeline: MFC: Hour Glass cursor problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC