Pointer problem?

S

Sh0t

Everytime i compile this program the pointer seems to lose its data??
*fileToFix is suppose to change after entering fixFile() as a param

program ------------------------------------------------

#include <iostream.h>
void fileFix(char*[], char type);

int main() {
char* fileToFix[30];
*fileToFix = "file";

fileFix(fileToFix, 'g');

cout << flush << endl;
cout << "Finished File: " << *fileToFix << endl;
return 0;
}

void fileFix(char *fileToFix[], char type) {
cout << "*fileToFix = " << *fileToFix << endl;
int pos = 0;
bool goOn = true;
char suff[] = {'.','d','a','t'};
char fileFix[30];
char fCopy[30];

fCopy[0] = type;

strcpy(fileFix, *fileToFix);

for (int i = 1; i < 30; i++)
fCopy = fileFix[i-1];

for (int i = 0; i < 30; i++) {
if (((int)fCopy < 65) || ((int)fCopy > 90 && (int)fCopy < 97) ||
((int)fCopy > 122)) {
goOn = false;
pos = i;
}
if (!goOn)
break;
}

for (int j = 0; j < 4; j++)
fCopy[pos+j] = suff[j];

for (int i = pos+4; i < 30; i++)
fCopy = '\0';


*fileToFix = fCopy;
cout << "end: *fileToFix = " << *fileToFix << endl;
}

end of program -----

please helppppppppppppp

carl
 
T

Thomas Matthews

Sh0t said:
Everytime i compile this program the pointer seems to lose its data??
*fileToFix is suppose to change after entering fixFile() as a param

program ------------------------------------------------

#include <iostream.h>
void fileFix(char*[], char type);
Perhaps you meant (see below):
void fileFix(char *, char type);
or
void fileFix(char [], char type);
In these two examples, the first parameter is
a pointer to char, not an array of pointers.

In general, when passing arrays to function, one
passes the array (or a pointer to the first location
in the array) and the length. Since your function
does not have an array length parameter, I conclude
that you don't want an array of pointers.

int main() {
char* fileToFix[30];
Do you realize that you are declaring an array of
30 pointers?

Did you mean:
char fileToFix[30]; /* a C string of 30 chars */

Or perhaps you want:
std::string fileToFix;

*fileToFix = "file";
This is not what you wanted. This line takes a pointer
to the literal "file" and places it into first location
of the array fileToFix.

Some alternatives:
char fileToFix[30];
strcpy(fileToFix, "file");
or
std::string fileToFix;
fileToFix = "file";

fileFix(fileToFix, 'g');
This line depends on the true meaning of the first
parameter.
cout << flush << endl;
cout << "Finished File: " << *fileToFix << endl;
return 0;
} [snip]


end of program -----

please helppppppppppppp

carl

You need to differentiate between the following
concepts:
1. chars
2. an array of chars.
3. a null or zero terminated array of chars,
often called a C-style string.
4. The std::string type.
5. Constant string literals.

See the FAQs below for more information.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,200
Messages
2,571,046
Members
47,646
Latest member
xayaci5906

Latest Threads

Top