help with pointers
hi, im trying to write a program but am not that great with pointers. the following code produces an error: " error C2440: '=' : cannot convert from 'char' to 'char *' "
#include<iostream>
using namespace std;
void to_char(int value, char *a[]);
void main()
{
char* first[20];
char* second[20];
char sum[21];
int num_1 = 0, num_2 = 0;
cout << "Enter two numbers, separated by a space, to add together.\n";
cin >> num_1 >> num_2;
to_char(num_1, first);
to_char(num_2, second);
}
void to_char(int value, char *a[])
{
int holder = 0;
for (int i = 0; i < 20; i++)
{
if(value == 0)
break;
holder = value % 10;
char charact = '0' + holder;
a[i] = charact;
value = (value - holder)/10;
}
}
if i change the fourth to last line to: " *a[i] = charact; " the error goes away but i get a runtime error: "Unhandled exception at 0x0041c468 in lab 14.exe: 0xC0000005: Access violation writing location 0xcccccccc."
How can i fix this? I'm sure its something simple that i'm just overlooking.
# 1 Re: help with pointers
I think you've misunderstood something.
the * means a pointer and the [] means pointer wich means:
*a[] is pretty much the same as **a in most cases it is the same, and all cases you can cast between the two without problems (I know of anyway).
so what you are actually saying is "I want an array of strings" or "a pointer to a pointer of char" and what you want (I think) is "just" a normal string.
so you can instead do:
void to_char(int value, char *a)
and
char first[20];
and so on, I hope you understand what I mean.
The diffrence between using the empty brackets [] and the * is the compilers way of handling the pointers (I think), but I think someone like drkybelk or Danny would be better at explaining the exact diffrence.
# 2 Re: help with pointers
Hi,
I think your problem is, that you declare first and second as pointers to arrays and your function to take pointers to arrays. Although there is nothing illegal with you declaration, I doubt a bit that this is what you want.
Change your code to
char first[20];
char second[20];
...
void to_char(int value, char *a)
...
and make sure that the result of the function (basically the array that 'a' points to) is '\0' - terminated.
That should work.
Principally you can use pointers to arrays of course, but as always with pointers, make sure that they are properly initialized/allocated/assigned/destroyed.
First look how you get on with this. One step after the other ;-)
Cheers,
D
# 4 Re: help with pointers
because he would get a unwantet result.
a char is a numeric representation of a letter (or sign) and can only have 255 values and a int is only the number and is larger in size.
and a string is an array of chars so if I cast an int to a string i would get some of the int in the first char and some in the next and so on would be a very weird result. :)
# 6 Re: help with pointers
You can make a copy constructor if thats what you mean so you got like:
char *mychararray = new char[1000];
Mystring varname = mychararray;
or
Mystring varname;
char *mychararray = new char[1000];
varname = Mystring(mychararray);