Use of pointers to assign values to a string array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

Use of pointers to assign values to a string array

 
0
  #1
Sep 16th, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,686
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 265
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
1
  #2
Sep 16th, 2009
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.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

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

 
1
  #3
Sep 16th, 2009
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.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

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

 
0
  #4
Sep 16th, 2009
Originally Posted by Lerner View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

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

 
0
  #5
Sep 16th, 2009
Originally Posted by Sky Diploma View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 247
Reputation: sfuo is on a distinguished road 
Solved Threads: 29
sfuo's Avatar
sfuo sfuo is offline Offline
Posting Whiz in Training

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

 
0
  #6
Sep 16th, 2009
I'm not 100% sure what you are asking is this correct?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,634
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 472
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

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

 
1
  #7
Sep 16th, 2009
You misspelt cout,
  //arrcon[1]=*scon;
 //count << "arrcon[1] " << arrcon[1];

string arrcon[5][35]; is a two-dimensional string array,
  1. arrcon[0][0]="Hello";
  2. arrcon[0][1]=*scon;
  3. cout << "\n" << arrcon[0][0] << " " << arrcon[0][1];
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 55
Reputation: yonghc is an unknown quantity at this point 
Solved Threads: 0
yonghc yonghc is offline Offline
Junior Poster in Training

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

 
0
  #8
Sep 18th, 2009
Originally Posted by adatapost View Post
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.

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC