I am experimenting with the single dimension array, using char string and then try to pull out the address of the first array member to use with a pointer. Then want the pointer to increment to the next array member. But so far cant get past this.
char str [10] [80]
int *p = &str;
the compiler gives, cannot convert char [80] [10]*' to ' int *'
so is it having a problem with the EOF character or what else
is causing a problem here?
I am wanting to get a pointer to point to the address of the first unit of the array and then index or be able to set it to to the next "row" or any row first unit of the row, since I was reading up on array's thought this should work. If there are 10 rows and 80 columns for data entry, well 79 columns, as I understand the last 80th location in each would contain another character required , in this
example. Each row being a char string up to the limited length of 79 chars in this instance. This was promoted as being one way to setup a means for your program to respond with an error message or something like this, by setting a pointer location and then printing the contained string. Thought this was supposed to be easy to setup, so was experimenting with it. Well at the time I was trying to setup a pointer, and thinking it needed to be int to hold an address location about to be put into it, from the &str, but maybe this is just another wrong way to view how this works. Thinking now, since its a pointer, the compiler must know it has to hold an address, so the only purpose for defining the type is because the compiler wants to organize data in certain ways, string, char, int, in their own respective areas for storage to follow certain rules, seems I read something like this. Anyway, just looking for how this should be done to get it to work.
char str[10][80] = {{"str1"},{"str2"},...,{"str10"}};
You could use a pointer to type char for pointing to string #k char* pStr = str[k];// where k=0,1,2...,9
pStr now holds the address of the 1st element of string #k ( ie &str[k][0] )
pStr can now be used to point to letter j in this string
(pStr + j) and &str[k][j] refer to the same memory location.
The character value stored there could be de-referenced in either pointer notation as *(pStr + j)
or in array notation as pStr[j]. Both are equivalent . Either way you are getting the character str[k][j].
Heres a console app showing that it works to use either pStr or str[j] to print the desired string.
Actually, I could use some pointers re. my input processing methods. It works but I suspect the methods are sloppy. I tested this. It actually works.
#include <iostream>// for use of cin, cout
#include <cstdio>// for use of gets(), scanf()
usingnamespace std;
int main()
{
char str[5][80] = {{"message one"},{"message two"},
{"message three"},{"message four"},{"message five"}};
int j = 0;// message # to print
char repeat = 'y';
cout << "This program is to print a selected message from a list of 5 messages\n";
cout << "enter a number from 1 to 5 when prompted\n";
do// until user is done printing messages
{
cout << "enter message# to print: ";
cin >> j;
if( j>=1 && j<=5 )
{ // here the message is displayed twice to show equivalence of methods
char* pStr = str[j-1];
cout << str[j-1] << endl;// actual array index to be 0 to 4
cout << pStr << endl;
}
else
cout << "the number must be 1,2,3,4 or 5" << endl;
cout << "repeat (y/n)? ";
_flushall();// my methods here may be off but it works
scanf_s("%c", &repeat);// read for a single character
_flushall();// find the right way to process input!
}while( repeat=='y');
return 0;
}