Pointers

The output contains wierd shapes .... can somebody explain? plz ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


int main(){
	
	char *ar;
	
	cout << "Enter size of the array" << endl;
	int size;
	cin >> size;
	ar = new char[size];
	srand(time(NULL));
	for( int i = 0; i < size; i++){
		ar[i] = (char)rand() % 40;
	}
	
	
	for( int i = 0; i < size; i ++){
		
		cout << &ar[i] << " \n";
		cout << *(&ar[i]) << endl;
	}
	
}
i think it might be line 25. you should probably just make it *(ar[i]), otherwise i believe the other way it tries to dereference the address. i suggest also taking a look at std::array and the std smart pointers.
Line 18: You're generating a random character from 0 - 39. The first 32 values are control characters.
http://www.asciitable.com/

Which ASCII characters are you trying to generate?

Line 24: You're going to output the address of a character. Is that what you intended?
closed account (18hRX9L8)
Online program for ease-of-use: http://coliru.stacked-crooked.com/a/e7cd591d0b8ebe73 .
Last edited on
Topic archived. No new replies allowed.