pointers help

This code is giving me an error.I am new to pointers.Which part of the string will the pointer tell the memory address ofand why is this code wrong.
#include<iostream>
using namespace std;
void main(){
char char1[13];
char *pointer ;
pointer=&char1[13];
*pointer="qwertyuiopas";//there is error here



cout<<pointer;
}
first of, you're using void int() {}, my compiler doesn't accept it. it's C, not C++.

I don't know about C but in C++, array names are constant pointers, so you don't need to use address of (&) operator.
also,
as far as I know, you can not assign values directly (except for using cin or other console inputs). you should use something like strcpy().

here is a working version:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<cstring>

using namespace std;

int main(){

	char char1[13];
	char * pointer;
	pointer = char1;

//	*pointer="qwertyuiopas";//there is error here

	
	strcpy(pointer, "qwertyuiopas"); //copy string to pointer


	cout << "pointer: " << pointer << endl;
	cout << "char1: " << char1 << endl;
	return 0;
} 

Last edited on
*pointer="qwertyuiopas";//there is error here

At this point in your code, *pointer is referring only to char1[0], which can only hold one character. You are trying to input a string of characters into a one byte cell.
closed account (zb0S216C)
Maese is right. What you need is a pointer to an array. You can declare one like this:

char( *Pointer )[ 13 ];
Pointer = &Char1;


Wazzak
Topic archived. No new replies allowed.