473,626 Members | 3,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer problem?

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(fileToF ix, '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 helpppppppppppp p

carl
Jul 23 '05 #1
1 1225
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(fileToFi x, "file");
or
std::string fileToFix;
fileToFix = "file";


fileFix(fileToF ix, '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 helpppppppppppp p

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.l earn.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2134
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived classes
5
6049
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user hits the right and left keys to move this insertion point (cursor) Here is the problem:
7
5177
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
10
4108
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr; };
204
12981
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
7
2041
by: Marcelo | last post by:
Hi everybody, I don't understand why I am having a problem in this code. The problem is that my pointer *phist in main method, it is declared. Then I send the pointer to my method, and this method creates a new object (a Matrix) for it. I suppose that after the new operator, my pointer is pointing to an object, so when the method has finished, the very first pointer is still poitint to the created method; however this is not working,...
51
10358
by: Joe Van Dyk | last post by:
When you delete a pointer, you should set it to NULL, right? Joe
2
2837
by: toton | last post by:
Hi, This is continuation of topic pointer & reference doubt. http://groups.google.com/group/comp.lang.c++/browse_thread/thread/df84ce6b9af561f9/76304d7d77f6ccca?lnk=raot#76304d7d77f6ccca But I think it is better to have a new topic rather than continuing on old one. As now I am sure pointer to reference and reference to pointer are freely convertable, the potential danger lies in the first one. Pointer may be NULL, but reference should...
9
3012
by: junky_fellow | last post by:
Hi, To print the pointer using printf(), we convert it to (void *) . printf("%p",(void *)ptr); My question is how printf() determine which type of pointer is passed to it and prints its value accordingly ? I have this doubt as different pointers may have different representation and different size. So, how does printf determine
6
2511
by: worlman385 | last post by:
For pointer and non-pointer initialization of an object like MyCar mycar; MyCar* mycar = new MyCar(); I heard from other people saying if object i create must live outside scape, then I use pointer version, else if only use object for a limited scope, then use non-pointer version. Does limited scope means the object is only used in the same function
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8199
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8705
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.