pointers...
Hi!
I''ve got a problem concerning C++ (I''m newbie)
I want to create a list:
void func1(int *list, int n_elems)
{
// Here I want to create the list with n_elems elements with
// ''new''
list = new int[n_elems];
}
void func2(void)
{
int *list;
char c[64];
func1(list, 10);
sprintf(c, "%d", list[0]); // Here it crashes
}
I can''t access the dynamic array I''ve created in func1. There
must be something wrong with the pointers.
Could someone please help me, I''m really new to C.
Thanks in advance
cu
Your problem is that modifications to the 'list' parameter aren't getting out of 'func1'
Either:
1) Change the 'int *list' parameter to '**list' and call func1 like so - 'func1(&list,10)'
2) Change the 'int *list' param to 'int &*list' (or *&list, can't remember which)
3) make your function return the pointer
int *func1(int elems)
{
return new int[elems];
}
Or look into the STL, and don't forget 'delete[] list' somewhere
Jans.
-----------------
Janucybermetaltvgothmogbunny
Edited by - Jansic on September 1, 2000 12:41:27 PM
Either:
1) Change the 'int *list' parameter to '**list' and call func1 like so - 'func1(&list,10)'
2) Change the 'int *list' param to 'int &*list' (or *&list, can't remember which)
3) make your function return the pointer
int *func1(int elems)
{
return new int[elems];
}
Or look into the STL, and don't forget 'delete[] list' somewhere
Jans.
-----------------
Janucybermetaltvgothmogbunny
Edited by - Jansic on September 1, 2000 12:41:27 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement