By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,669 Members | 1,654 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,669 IT Pros & Developers. It's quick & easy.

Pointer problem?

P: n/a
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[i] = fileFix[i-1];

for (int i = 0; i < 30; i++) {
if (((int)fCopy[i] < 65) || ((int)fCopy[i] > 90 && (int)fCopy[i] < 97) ||
((int)fCopy[i] > 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[i] = '\0';
*fileToFix = fCopy;
cout << "end: *fileToFix = " << *fileToFix << endl;
}

end of program -----

please helppppppppppppp

carl
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Sh0t wrote:
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

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.