| | |
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:
Solved Threads: 0
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.
//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)
// pointers Use of pointers to assign values to a string array #include <iostream> #include <stdio.h> #include <conio.h> #include <string> #include <stdlib.h> using namespace std; int main () { string arrcon[5][35]; string* scon; string name; string xcon; name="YBY"; scon=&name; xcon=*scon; cout << "\nname = " << name; cout << "\nscon= mem add. of name: " << scon; cout << "\nxcon= value of name: " << xcon << "\n\n"; // Now using array //arrcon[1]=*scon; //count << "arrcon[1] " << arrcon[1]; // use of pointers courtesy of http://www.cplusplus.com/doc/tutorial/pointers/ cout << "\n\nUse of pointers courtesy of http://www.cplusplus.com\n\n"; int numbers[5]; int * p; p = numbers; *p = 10; p++; *p = 20; p = &numbers[2]; *p = 30; p = numbers + 3; *p = 40; p = numbers; *(p+4) = 50; for (int n=0; n<5; n++) { cout << "\t" << numbers[n] << ", "; } cout << "\n\n"; getch(); return 0; }
•
•
Join Date: Jul 2005
Posts: 1,686
Reputation:
Solved Threads: 265
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.
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
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.
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.
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
•
•
•
•
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.
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?
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
•
•
•
•
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.
say name="John", weight="56", store these variable values into arrcon[1], arrcon[2]. What are the C++ syntax statements to perform this assignment?
I'm not 100% sure what you are asking is this correct?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> using namespace std; int main() { string arrcon[2]; arrcon[0] = "John"; arrcon[1] = "56"; for( int i = 0; i < 2; i++ ) { cout << arrcon[i] << endl; } system("PAUSE"); return 0; }
You misspelt cout,
string arrcon[5][35]; is a two-dimensional string array,
//arrcon[1]=*scon;
//count << "arrcon[1] " << arrcon[1];string arrcon[5][35]; is a two-dimensional string array,
C++ Syntax (Toggle Plain Text)
arrcon[0][0]="Hello"; arrcon[0][1]=*scon; cout << "\n" << arrcon[0][0] << " " << arrcon[0][1];
Failure is not fatal, but failure to change might be. - John Wooden
•
•
Join Date: Aug 2009
Posts: 55
Reputation:
Solved Threads: 0
•
•
•
•
You misspelt cout,
[code]
string arrcon[5][35]; is a two-dimensional string array,
[code]
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)
// pointers3 YBM Use of pointers to assign values to a string array #include <iostream> #include <conio.h> #include <string> using namespace std; int main () { string arrcon[5][35]; string* scon; arrcon[0][0]="Hello"; scon = &arrcon[0][0]; arrcon[0][1]=*scon; cout << "\n" << arrcon[0][0] << " via pointer= " << arrcon[0][1]; cout << "\n\n"; getch(); return 0; }
![]() |
Similar Threads
- data string to array (C#)
- split and assign values in a file. (Perl)
- String array problem (C++)
- comparing stings to string array values (C++)
- Storing and Retrieving Number String/Array (PHP)
- string array (Java)
- Assigning Array Values En Masse (Java)
- Declaring string array, initializing later... (C++)
- How to assign my own numeric values to a char array? (C++)
Other Threads in the C++ Forum
- Previous Thread: Read in/Write from Memorystream
- Next Thread: MFC: Hour Glass cursor problem
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






