Member Avatar for yonghc

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.

// 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; }
Member Avatar for 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.

commented: Thnks. i got it.+1
Member Avatar for Sky Diploma

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.

commented: Thanks. got mixed up with c-string array.+1
Member Avatar for yonghc

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?

Member Avatar for yonghc

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?

Member Avatar for sfuo

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

#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; }
Member Avatar for kvprajapati

You misspelt cout,

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

string arrcon[5][35]; is a two-dimensional string array,

arrcon[0][0]="Hello"; arrcon[0][1]=*scon; cout << "\n" << arrcon[0][0] << " " << arrcon[0][1];
commented: Thanks, I got it.+1
Member Avatar for yonghc

You misspelt cout, string arrcon[5][35]; is a two-dimensional string array,

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.

// 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; } 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.