-
Is this right
Ive been trying to do this for a while
and still cant figure it out, im probably
going about it the wrong way
Code:
int size;
char test[] = "hello world this is a great day";
size = strlen(test);
my_function(test,size);
void my_function(char *string, int sizer)
{
char new_string[sizer];
return(0);
}
I want to allocate the size of new_string dynamically
can this be done?
Marky_Mark
-
You'd have to do -
char* new_string = new char[sizer+1];
strcpy(new_string,string);
and after you've finished with new_string(don't let it go out of scope unless you're copying/returning it) you need to free the memory -
delete[] new_string;
new_string=NULL;
-
Thanks zen
I am nearly pulling my hair out, i need to pass
a filename to a function, but am strugling
How do i get the whole filename and use it in my function
It's accessing words as a whole that im struggling with
surely i don't have to loop through an array to
get my filename
Marky_Mark
-
Do a search through the boards... I remember exactly that topic being discussed at least once recently.
-
Ive been searching all day, and all i can find
is how to access strings through looping
Id like to point straight to my string and use
this as a whole variable, is this possible without
looping
Marky_Mark
-
Prototype
void open_thisfile(char *filename);
Function
void open_thisfile( char *filename ) {
// blah
fp = fopen(filename,"r");
}
main
char file[] = "test.txt";
open_thisfile(file);
-
I was raised to think that you had to declare the function before the function call...Is that not true?!?!
-
Thanks Salem
:D
Marky_Mark
-
> I was raised to think that you had to declare the function before the function call...Is that not true?
Either the function declaration (or it's prototype) should be seen before you try and use a function.
-
return VALUE
:eek:
void my_function(char *string, int sizer) {
....
return (0);
....
}
what does this mean anyway?
Does it create a new space within the RAm ur are just playing with it.
-
hey Salem, quick question, how do you print the contents of a file on your program with your technique? im just curious....thanks:rolleyes:
-
zen you talked about freeing up the space held
by my objects, how do i free these memory resources
Code:
int a;
int *ptr;
Test* X = NULL;
X = new Test;
X->set_id(10);
a = X->get_id();
ptr = &a;
I need to free *ptr and which object do i need to free X or Test i don't know
Some correct syntax would be nice for standard pointer and object
Marky_Mark
-
-
NO NO NO
delete ptr;
delete X;
-
Can anyone shed some light on what's happening
Ive added
Code:
delete ptr;
delete X;
To my program and it crashes, do i need to
add a memory header to use delete
Im using Visual C++ 6.0
Marky_Mark