LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   what is wrong with this pointer thingy (https://www.linuxquestions.org/questions/programming-9/what-is-wrong-with-this-pointer-thingy-150500/)

Longinus 02-25-2004 05:45 PM

what is wrong with this pointer thingy
 
hi

i am new to pointers and i am having some trouble with this:

----------------------------------------------------
#include <iostream>
using namespace std;

typedef struct{
char alpha[26];
short num;
}MyStruct;

int main(){

MyStruct ms1;
MyStruct *pms1 = &ms1;

pms1->alpha = "abcdefghijklmnopqrstuvwxyz";
pms1->num = 5;


cout << "The memory address of num is: " << &(pms1->num) << endl;
cout<< "num is: " << pms1->num << endl;

return 0;
}
------------------------------------------------------

the error appears o nline 14 according to g++:

--------------------------------------------------
pointers7.cc: In function `int main()':
pointers7.cc:14: incompatible types in assignment of `const char[27]' to `
char[26]'
-----------------------------------------------

if anybody could tell me whats wrong that would be awesome

thanks

ToniT 02-25-2004 06:13 PM

Your array is too sort, you have forgotten the null character.

Also you can't do array assignments this in C++, only in C. Use one of: strcpy (see man strcpy), char pointer instead of statically 27 characters or C++/STL string class

BTW: when pasting code and referring to line numbers, please pipe it through nl.


All times are GMT -5. The time now is 11:52 PM.