?
Solved

Problem maintaining pointer assignment

Posted on 2003-03-11
9
Medium Priority
?
206 Views
Last Modified: 2010-04-01
Here's what I'm trying to do - I read a file line-by-line and I check each line to determine if it is the start of a section, the end of a section or neither.  My section heading and endings (start and end) are stored in a linked list.  The problem is the following:I have to pointers to the linked list. One pointer always points to the head of the list. The other pointer is either null or points to the "link" where the section head was found.  So, I read in a line from the file, call the processFile function. The head and current pointers are passed to the function.   If a section start is found, I expect that the curr_obj pointer would be set to point to the appropriate link. But it doesn't. After the function runs, curr_obj is always NULL, even when a start is found.  I'm not sure what's wrong with my logic. I'd appreciate any help.

//test.h
struct obj {  //
  char *start;
  char *end;
  obj *next;
}

class Test {
public:
void main(char *filename);
private:
boolean processLine(char *line, obj *list, obj *current);
}

//Test.cpp

boolean Test::processLine(char *line, obj *list, obj *current) {
  bool result = false;
  if (current != 0) {   //start found, check for matching end
      if (strstr(line, current->end) != NULL) {   //search for "end" in "line"
             current = 0;
             result = false;                      
      }
      else
             result = true;
  }
  else {          //check for start
     current = list;
     while (current != 0  && !result) {
          if (strstr(line,current->start) {
             result = true;
       }
          else
             current = current->next;
     }
  }
  return result;
}//end processLine

void Test::main(char * filename) {
   char _line[80];
   obj *head = 0;
   obj *curr_obj = 0;
   bool inSection = false;
   
  //open file...
  //read in line of file...
  while (!EOF) {
      ...
      inSection =  processLine(_line, head, curr_obj);
      if (curr_obj != 0) {   // if start was found in function, curr_obj should point to "link" in the list
         ...
       }
      //read in next line of file ...
  }

}//end main
0
Comment
Question by:apatia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 5

Expert Comment

by:Kocil
ID: 8115898
/* add double ** on current */
/* use *current inside the function */
boolean Test::processLine(char *line, obj *list, obj **current) {
 bool result = false;
 if (*current != 0) {
     if (strstr(line, *current->end) != NULL) {
            *current = 0;
            result = false;                      
     }
     else
            result = true;
 }
 /* and so on */

 ...
}//end processLine


/* call it by using & */

void Test::main(char * filename) {
  char _line[80];
  obj *head = 0;
  obj *curr_obj = 0;
  bool inSection = false;

   ...
   inSection =  processLine(_line, head, &curr_obj);
   if (curr_obj != 0) {  
        ...
   }
 }

0
 
LVL 5

Accepted Solution

by:
Kocil earned 1000 total points
ID: 8115936
Oh man ... this is C++, not C :)
This is the better solution.

// Add & on current
// don't change anything else
boolean Test::processLine(char *line, obj *list, obj* & current) {
 ...
}
0
 
LVL 1

Expert Comment

by:yukapapa
ID: 8116816
Hi, apatia.

The variable curr_obj defined in the main function remains 0 after calling the processLine function because the function receives a copy of a value of the curr_obj as the argument current. Therefore you can not change the value of curr_obj in the main through the current in the processLine. In general, this calling method is called as "call by value".

You can change the curr_obj in the processLine by using "call by reference". For that purpose, use a pointer or reference to the variable.

Using reference, processLine can be defined as follows:

boolean Test::processLine(...., obj*& current){
...
}

Now the current is an alias of the curr_obj, you can change the curr_obj's value through it and no other modifications are required for the codes. A notation "type& ref" declares ref as a reference to a variable of the type.

Using pointer, processLine can be defined as follows:

boolean Test::processLine(...., obj** current) {
...
}

In this case, the current points to curr_obj and you can change the curr_obj's value through the pointer. But you need to change the notation of the current to *current in the processLine and the processLine(...., curr_obj) to processLine(...., &curr_obj) in the main.

You can use whichever of reference and pointer as your preference. Using pointer is my preference because it makes clear that the function modifies the pointed object.

Here's a sample code:

#include <iostream>
#include <string>

//call by value
void f(std::string s){
s = "f";
}

//call by reference using a reference
void g(std::string& s){
s = "g";
}

//call by reference using a pointer
void h(std::string* s){
*s = "h";
}

int main(){
std::string s = "original value";

f(s);
std::cout << "s = " << s << " after f(s) was called.\n";

g(s);
std::cout << "s = " << s << " after g(s) was called.\n";

h(&s);
std::cout << "s = " << s << " after h(&s) was called.\n";

return 0;
}

Sample code output:

s = original value after f(s) was called.
s = g after g(s) was called.
s = h after h(&s) was called.

Hope this helps.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 5

Expert Comment

by:Kocil
ID: 8116886
Well long written yukapapa.
I was too lazy :)

0
 

Author Comment

by:apatia
ID: 8116909
Kocil - thank you for the quick response. Your second answer worked for me.

Yukapapa - thank you for the thorough explanation.

I will accept Kocil's comment as the answer AND I will post a request to have Yukapapa awarded  250 since the comment was so informative.

Thanks again everyone!
0
 
LVL 1

Expert Comment

by:yukapapa
ID: 8117077
I'm sorry to repeat the Kocil's comments. I didn't know Kocil made answers for the question. As he indicated, my comment is too long to describe the solution. In fact, I am not a native English speaker. I am posting comments for the questions to learn English, not for points. I was afraid that the comment might include ill English. But I feel relieved to hear from apatia and Kocil. Thanks.
0
 
LVL 5

Expert Comment

by:Netminder
ID: 8117184
Per request in http://www.experts-exchange.com/Community_Support/Q_20547275.html the points in this question have been reduced from 500 to 250.

apatia,

Here's what you do now:
1. In this topic area, ask a new question worth 250 points entitled "For yukapapa re: 20547103" and put the URL to this question in the body.
2. Come back to this question and accept the comment of Kocil as an answer. Leave a comment that directs yukapapa to the "points for" question.
3. When yukapapa comments in the new question, accept that as an answer.

That should take care of you.

Netminder
EE Admin
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8117977
Wow the best typing/point for me so far.

one '&' worth 250 point !

Thanks man, and give me and A please.
0
 

Author Comment

by:apatia
ID: 8119600
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question