C++ :: Linked List Node Passed As Parameter / Argument Pointer To Pointer Notation

Jan 17, 2014

I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this

bool deleteNode(struct node *head, struct node *delptr)

I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.

Would also like to know why the argument call needed &head instead of just head.

remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);

#include "stdafx.h"
#include<iostream>
struct node{

[Code].....

View 1 Replies


ADVERTISEMENT

C++ :: Pointer To Last Node Of A Doubly Linked List

Nov 30, 2014

Why is it sufficient to only have a pointer to the last node of a doubly linked list?

View 1 Replies View Related

C/C++ :: Pointer To Last Node Of A Circular Linked List

Nov 30, 2014

Why is it sufficient to only have a pointer to the last node of the list?

View 3 Replies View Related

C/C++ :: Freeing Linked List Node With Pointer?

Mar 30, 2015

I'm having a bit of an issue here. I have a linked list where each node contains a pointer to a string (which has been malloc'd when the node was created and inserted) and a pointer to the next node in the linked list.

I'm creating a function which will free the node (or effectively delete it). However, I'm receiving a free(): invalid pointer error.

My function:

void removeNode(node *(*nodeToRemove))
{
free((*nodeToRemove)->data);
(*nodeToRemove)->next = NULL;
free(*nodeToRemove);
}

Is this how I should go about freeing this node?

View 5 Replies View Related

C :: Modifying Linked List - Passing Pointer As Argument

Feb 27, 2015

I am having trouble modifying a linked list. I am writing a function to delete the last node from the linked list, but it gave me incompatible types error.Here is my struct:

Code:
typedef struct PCB{
int id;
struct PCB *next;
struct PCB *prev;
}PCB_rec, *PCB_p;

Here is my function to delete the last node (given the pointer is pointing at the last node of the list):

Code:
void del_last_node(PCB_p *process_list){
PCB_p temp = process_list;
if (temp->prev != NULL){
temp = temp->prev;

[Code] ....

And here is how I called the function:

Code: del_last_node(&process_list);

It gives me the following errors:
initialization from incompatible pointer type at line:
PCB_p temp = process_list
assignment from incompatible pointer type at line:
process_list = temp

View 14 Replies View Related

C++ :: Maintaining Additional Pointer To First Node In Doubly Linked List

Nov 30, 2014

Why would it be cumbersome to maintain an additional pointer to the first node in a doubly linked list?

View 1 Replies View Related

C++ :: Function Having Argument As A Pointer Passed By Reference

Dec 8, 2014

I have to write an example in which you use a function having as argument a pointer passed by reference in C++. Can you provide an example like this:

funz.h : void funz( int *&a );
funz.cpp : ? (1)
main.cpp:
#include "funz.h"
#include <iostream>

[Code]...

as I write in (1) and (2) ?

View 5 Replies View Related

C :: Insert A Node At Passed Index In Linked List

Feb 17, 2013

I'm still trying to insert a node at a passed index in a linked list. This is my function, it doesn't work but i feel like it's close.

Code:
void llAddAtIndex(LinkedList** ll, char* value, int index) {

LinkedList* newNode = (LinkedList*)malloc(sizeof(LinkedList));
newNode->value = value;
LinkedList* prevNode = *ll;
for(int i = 0; i < index; i++){

[Code] ....

View 2 Replies View Related

C++ :: Object That Is Passed To Function Is Changed Although No Pointer Is Passed

Mar 22, 2013

I am posting this simplified piece of code that is a bit confusing for me. There are two functions that I call. One shows the expected results but the result of the other one rather puzzles me.

//#define defineVecTyp Vec3f
#define defineVecTyp float
template <typename vecTyp>
vecTyp buildLaplacianPyramid(cv::Mat inputmat) {
vecTyp lapPyr;

[Code].....

Calling the function sum1 does not change the values stored in the variables val1 and val2. The output of the program is as follows:

val1= 1 ## val2= 10 // before the call of function sum1
val1= 1 ## val2= 10 // after the call of function sum1
sumOfVals= 22

This is quite obvious and as expected and I just pasted this piece of code as an example for better clarification.

However, if I call the function buildLaplacianPyramid and apply a function for Gaussian Blurring, this also effects the cv::Mat passed to the function. The line imshow("M1, after buildLaplacianPyramid",M1); therefore shows an image that is blurred. Since I am not passing a pointer to the cv::Mat I do not understand why this should be happening. I was assuming that there would be a copy of the cv::Mat M1 to be used within the function. Therefore I was expecting the cv::Mat M1 to retain its original value. I was expecting that all changes applied to cv::Mat inputmat within the function would not have any influence on the cv::Mat M1. Just like in my other example with the sum.

View 3 Replies View Related

C/C++ :: Push Linked List Pointer

Jan 27, 2015

I've been playing around with making a linked list in C and am having trouble adding to the beginning of the list. For the add_beg() function: the statement "head = newNode" only works locally. This leads me to believe this is a pointer problem. However, the add_end() function works correctly. Ideally, I would like to print "0 1 2 3 4" after using the add_end(root, 0). Currently, print only gives me "1 2 3 4".

#include <stdio.h>
#include <stdlib.h>
//Create a node struct
typedef struct node {
//data
int val;

[Code] .....

View 11 Replies View Related

C++ :: Infinite Loop For Pointer Variable In Linked List

Jan 14, 2014

I am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.

#include "stdafx.h"
#include<iostream>
struct node{
int data;
node *next;

[Code] .....

View 1 Replies View Related

C++ :: Output Sorting Error Using Pointer Notation

Aug 6, 2012

My code compiled well(After long Messing up with my head). But, i still not satisfied of my output as i expected. My code ought to sort the object of person comparing their salary. But, its not.

Code:
#include <iostream>
#include <string>
using namespace std;
class person {
protected :
string name;
float salary;

[Code] ....

It doesn't sort the object of class person rather than it prints out the stored value as it is.

View 3 Replies View Related

C++ :: Linked List Set Function - Forbids Comparison Between Pointer And Integer

May 5, 2014

I'm trying to write a function called 'set' that sets the value of the i'th cell to val on my linkedList, and then returns the previous contents. I am stuck on it due to compiling errors:

This is my Node struct:

#ifndef NODE_H
#define NODE_H
#include <iostream>
template <typename T>
struct Node {
friend std::ostream &operator <<(std::ostream &os, const Node<T> &node) {

[Code] ....

The following is my set function:

template <typename T>
T set(Node<T> *head, int i, const T &val) {
for(int n = 0; n < i; n++)
if(head == val) {
val = i;
} return val;
} #endif

When I try to call it in the main() I get these errors:

node_utils.h: In function ‘T set(Node<T>*, int, const T&) [with T = int]’:
node_demo.cpp:26:38: instantiated from here
node_utils.h:161:2: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
node_utils.h:162:3: error: assignment of read-only reference ‘val’

So, I understand that I can't compare head & val on my if-statement -- But what do I do?

View 7 Replies View Related

C++ :: Generic Linked List Compilation Errors / Incompatible Pointer Type

May 19, 2014

I am trying to write a generic linked list in c, but for some reason i keep getting errors saying "incompatible pointer type. This is the code and erros:

#ifndef SLIST_H
#define SLIST_H
#include <stdlib.h>
typedef struct {
void *data;
slist_elem *next;

[code]....

View 2 Replies View Related

C++ :: Pointer Passed To Function Value Doesn't Change

Dec 24, 2014

when i pass a string pointer to a function such as string *str = new string(""); and pass that string to a handleElement() function e.g. handleElement(str), and i change the value in the function it simply doesn't change when the function exits as though it's passing it by value or something, even though it gives the pointer address.. I've now changed the code to use double pointers and pass the reference of the str pointer and it works but it seems ugly to use double pointers for this.

//handles when a new element is encountered when parsing and adds it to the parse tree
bool ParseBlock::handleElement(char cur, string *curString, int count, bool isOperator) {
countNode = new ParseNode(count);
//keep track of numbers and strings if they exist and insert them
if(!curString->empty()){
if(isNumber(*curString)

[code].....

View 2 Replies View Related

C++ :: Pointer Passed Into A Function Looses Address It Points To

Mar 7, 2013

Sem is a pointer to semantic which is a struct type variable. I pass the sem into function yylex so i can fill the semantic.i and semantic.s(s points to an array). The problem is that when sem->i = a; is used inside yylex function, sem->s stops showing to the array.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
using namespace std;
union SEMANTIC_INFO

[Code] ...

View 2 Replies View Related

C/C++ :: Add Node In Linked List?

Dec 5, 2014

I create A program that will Add and Display the Link List but I have problem on it. If I add it will inserted in the prev node not in the next node. Here's my source code.

#include <stdio.h>
typedef struct Member {
int id;
char name[256];
struct Member *next;

[Code] ....

View 2 Replies View Related

C++ :: Using API Function That Has Char Pointer As Argument

Feb 5, 2014

I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:

BASEBOARD_ERROR_KIND ZMP zrc :: :: :: Baseboard GetRS232Data (char * msg )

RS232 data acquisition.

Argument:
[Out] msg Address of the acquired data.

Returns:
BASE_OK RS232 data acquisition success
BASE_BASE_232_GETDATA_ERR RS232 data acquisition failure

I have trouble writing the relevant code in the main program that invokes this function. Here is a snippet of what I have tried:

# include "Baseboard.h"
int main () {
Baseboard _Baseboard; // Class name is Baseboard
char *msg ;

[Code] ......

The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?

Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value) .

View 2 Replies View Related

C++ :: Void Pointer Argument In A Function?

Jun 11, 2014

Why does the following code compile and execute without any error? I mean, the function compareid should get 2 arguments so why does the compiler not complaining, is it because of the type of arguments?

Code:
#include <stdio.h>
int compareid(void* info, int value); // ansi declaration
int compareid(void* info, int value)

[Code] .....

View 5 Replies View Related

C :: Deleting A Node From Linked List

Apr 19, 2013

I have a struct with some select student information. My wish is to be able to have the user type in a surname from the list, and for that entry to be deleted. However I am slipping up for some reason.

Here is the start of my program showing my struct:

Code:
#include <stdio.h>
#include <string.h>
#define NAME_LEN 30
#define LINE_LEN 80
struct record {

[Code] ....

I get the following errors in my while loop as well as the if statement below that loop:

-invalid operands to binary !=
-invalid type of argument '->'

View 7 Replies View Related

C :: Handling Linked List Node Add At End?

May 17, 2014

This is what I have at the moment which works as I would like but wondering if there is a better way of handling? Currently just making sure I know linked-lists well enough.

Code:

void push_to_end( node_t * headRef, int val ) {
node_t * tail = headRef;
node_t * new_node = malloc( sizeof ( node_t ));
while( headRef != NULL ) {

[Code].....

View 12 Replies View Related

C :: Linked List - Allow User To Add A New Node

Aug 12, 2013

I'm trying to impliment a simple singly linked list, then allow a user to add a new node. I have mocked up a siimple example to illustrate my point using Linked Lists...

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int x;
struct node *next;

[Code] ....

So I have done things similatr to this in C# and Java but not in C. There maybe some parts of this I'm sure some will disagree with in terms of buffers,overflow etc. but it's just the linked list part that I am interested in at the moment, I am only doing it like this because when it works, I will be extracting the working linked list stuff into another program that can deal with its I/O etc.

I have tried to comment as best I can to demonstarte my intentions and understandings per line of code. The add function needs to add a node of value x that the user has input, and add that to the end of the list - I'm sure that the print function is not doing all its supposed to do...

View 4 Replies View Related

C++ ::  linked List - Deleting A Node

Mar 2, 2013

If p is a pointer pointing a node to be deleted, then what's wrong in the following code:

cout << "Do you want to delete this record? (y/n) ";
if (getch() == 'y' || getch() == 'Y'){// Delete record.
if (p == *ph){//If first node is to be deleted.
*ph = (*ph)->next;
delete p;

[Code] .....

View 2 Replies View Related

C++ :: Linked List - Implementation At Nth Node

Feb 27, 2015

I have to write a program which has the user be able to enter a specific value at a specific position of the linkedlist, replacing that node with the user defined value

EX: Enter the value 5 at 2nd node, which will override the old value at the 2nd node with the new one

I am getting a compiler error which terminates my program right after the user presses the return key after he/she has given a position to change the value

Error: Unhandled exception at 0x013C50C1 in Linked(1).exe: 0xC0000005: Access violation reading location 0x0000812B.

I am not going to show the whole code as the problem resides solely on the insert function:

void TheNode::insert(double num, int choice) {
int post = 0;
MyNode *ptr = new MyNode;
(*ptr).value = num;
MyNode *previous = head;
MyNode *current = head->next;

[Code] .....

View 1 Replies View Related

C++ :: Append Node To A Linked List

Jan 27, 2013

I have a function that append node to a linked list like this:

struct ListNode{
int value;
struct ListNode* next;
};
void appendNode(struct ListNode* head, int num){

[code] ....

when I use it, the head in main() does not change its address and it's still pointing to NULL. Why??

View 5 Replies View Related

C++ :: Should Delete Every Node In Linked List?

Jul 10, 2014

Still toying with my self-coded linked list class and now another question: should I delete each node of my class with the delete in the class destructor or is it done automatically when the main() function ends?

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved