C :: Sorted List - Why Value Of Void Pointer Change

Feb 16, 2014

I have a the following in a header file.

Code:

struct SortedList{
void * data;
struct SortedList * next;
struct SortedList * previous;
int (*compareFunc)(void *, void *);
void (*destructor)(void *);

[Code] ....

In a .c file, I implemented the SLCreate function.

Code:
SortedListPtr SLCreate(CompareFuncT cf, DestructFuncT df){
struct SortedList item;
item.data = NULL;
item.next = (struct SortedList *) malloc(sizeof(struct SortedList));

[Code] ....

In main.c, I try to do the following:

Code:
SortedListPtr list = SLCreate(&compareInts, &destroy);

A bunch other code that does not alter list or it's contents at all.

struct SortedList item = (*list);
void * data = item.data;
if (data != NULL) {
printf(Why did data become not null???
"); }

How come my variable data became not null anymore when I haven't altered it at all....

View 2 Replies


ADVERTISEMENT

C++ :: How To Convert Void Pointer To Int / Double / String Pointer

Mar 7, 2013

I have a function:

const void insertStuff(const void *key, const int value){
// I want to convert the void pointer into one
// of three types of pointers(int, string, or double)
switch(value){
case 0:
int *intPtr = key;

[Code] .....

But this causes an error of: "crosses initialization of int*intPtr"

What's the correct way of implementing this?

View 1 Replies View Related

C :: Sorted Linked List

Apr 2, 2013

I been ask to create a sorted linked list module, and to test each function with 1 unit, 2 +.so i wrote my addtofront function( we have to use the same as the teacher)

Code:

struct MyList{
char * name;
Sorted * AddmyChar;
struct MyList * next;
}

[code]....

but i cant seem to test it because it always display a seg fault.... i dont know what i did wrong....how do i call this function in main to display a string i want?

View 9 Replies View Related

C++ :: Logical Error - Change Value Inside Void Statement

Jul 16, 2013

I've encountered a slight logical error in my code

/*
Lab06_pensionplans.cpp
Purpose :
- Create a simple financial application to calculate retirement plans
*/
#include <iostream>
#include <cstdlib>
using namespace std;
void displayMenu() {
system("cls");

[Code] ....

Look at case 2, which the user supposed to key in a new input, the problem is the value will never got into main function, I don't know what should I modify with the function.

Figured out I need to change the

void changeData(int startingAge, int numOfYears,
double lumpSumAmount, double yearlyAmount, double interestRate )

into

void changeData(int &startingAge, int &numOfYears,
double &lumpSumAmount, double &yearlyAmount, double &interestRate )

View 2 Replies View Related

C :: Sorted Linked List - Crashes On Malloc

Sep 29, 2013

So I've been working on a sorted linked list homework assessment and I've been stuck on a problem for a while now. Below is my code for inserting a new object into the linked list, for some reason it keeps crashing whenever I try to malloc temp. (between the "checkpoint" and "after malloc" printf statements) .

Code:

int SLInsert(SortedListPtr list, void *newObj){
SortedListPtr curr = list;
SortedListPtr temp = NULL;
if(list->obj == NULL) /*if the list is empty insert obj into the first node*/ {
list->obj = newObj;
free(temp);

[Code] ....

View 13 Replies View Related

C :: List Of Words That Need To Be Read In From A File And Then Sorted

Apr 4, 2013

I'm given a list of words that need to be read in from a file and then sorted. I was given a certain way to do it .

Code:
#include <stdio.h>
#define MAX 100
void print_ar(char x[][MAX], int size);
int strcomp(char x[], char y[]);
int strlength(char x[]);
void selection_sort(char c[][MAX], int size);
void strcopy(char x[], char y[]);

[Code] ...

Here are some words from the list

school
aibohphobia
lists
alula
bake
cammac
civic

I cannot figure out what is wrong at the moment. .

View 3 Replies View Related

C/C++ :: Adding Linked List In Sorted Order

Sep 22, 2014

I am currently trying to add to a linked list in sorted order but I have reached an impasse. I can get it to add in sorted order if it belongs in the beginning or second in the list. If i were to type in 9 then 4 i would get 49, but if i type in 5 it changes it to 559. I'm just at a loss and need some sort of direction.

#include "singly_linked_list.h"
#include <iostream>
using namespace std;
void add_node(node*& head_ptr, const int& payload){
if (head_ptr == nullptr) {
node* my_node = new node();
my_node->data = payload;

[Code]...

View 2 Replies View Related

C Sharp :: Error Concerning Sorted List Not Being Accessible Due To Its Protection Level

Oct 7, 2012

For school we have to create a blackjack game using windows form. I had to store the images of each card into a sorted list so i created a class called cardList and created a constructor which contained the the sorted list called cards. So it looks kinda like this:

public class cardList : Form1
{
SortedList cards = new SortedList();  
public cardList()

[Code]....

There's probably a few other errors, I'm still trying to figure this whole c# thing out. why the error tells me (on the line that contains c.cards.GetByIndex(cardNumber);) that cards is inaccessible due the its protection level.

View 1 Replies View Related

C++ :: Pointers And Arrays - Program To Display Sorted List And Average Of Scores With Appropriate Headings

Apr 22, 2014

I am writing a class that dynamically allocates an array that holds a user-defined number of test scores (test scores go from 0 to 10 both included). Once all the test scores are entered and validated (values only between 0 and 10, both included), the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates theaverage of all the scores.The main program should display the sorted list of scores and the average of the scores with appropriate headings.

View 6 Replies View Related

C :: Dereferencing Of Void Pointer

Apr 14, 2014

I could understand void pointers I created the following program:

Code:
#include <stdio.h>
#include <string.h>
int main(void) {

char word[] = "Zero";
int number = 0;
void *ptr = NULL;

[Code] .....

The program works fine, however i really want to fully understand what is going on with the dereferencing of the void pointer, for example: With the following code:

Code:
ptr = &number;
*((int *)ptr) = 1;

Why can't you just do:

Code:
ptr = &number;
*(int *)ptr = 1;

And again with this code, (i'm guessing it's becuase its a pointer to a pointer?):

Code:
ptr = &word;
strcpy(ptr,"One");

View 2 Replies View Related

C :: Printing From Void Pointer

May 26, 2014

Code:
int main() {
List* newList= lst_new();
names* nama;
char* data;
int x=1;

[Code] ....

I cant seem to be able to print a string.. the functions lst_next() lst_first() return void*.

View 9 Replies View Related

C++ :: Void Pointer As A Parameter

Mar 14, 2013

I want to have a function that has a pointer to an int/double/string so I thought I'd use a void pointer like so:

int someFnc(int a, int b, const void *key){
// take care of converting key into appropriate type in here
}

And when I want to use this function I'd like to be able to do something like this:

main{
...
int myKey;
someFnc(1,2,myKey);
]

But I get a compiler error telling me:

invalid conversion from 'int' to 'const void' -[fpermissive]

Do I need to convert myKey into a void pointer before passing it as an argument?

Why does passing myKey like this work?

someFnc(1,2,&myKey);

View 1 Replies View Related

C/C++ :: How To Use Void Pointer Functions

Sep 27, 2014

int (*cInts)(void*,void*);
cInts= &compareInts;

int x=(cInts)(2,5); //This wont work. I tried a bunch of other things
printf(x);

View 5 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++ :: Reference Pointer - Void Exchange

Mar 15, 2012

#include "vehicle.h"
...
void exchange(vehicle *&v1, vehicle *&v2) {
vehicle *tmp = v2;
v2=v1;
v1=tmp;
}

Is it right?

How about: void exchange(vehicle *v1, vehicle *v2){...}

What is the difference between *&v1 and *v1 ?

View 4 Replies View Related

C++ :: Return Struct Pointer From A Void Function And Print

Mar 17, 2013

i need to return a struct pointer dynamically allocated inside a function call void function() which is done using 'out parameters' in following code

struct my_struct {
int x;
} void my_function( my_struct** result ) {
my_struct* x = new my_struct{ 10 };
//...
*result = x;
}

Now i have a doubt, so if i want to print the return value from struct pointer, should i need to print it in the void function() or in the caller the function...

View 3 Replies View Related

C++ :: Cannot Cast From Void Pointer - Returns Always Error C2440

Apr 25, 2013

I having a problem which I'm not able to resovle. I try to dereference a void pointer but I always get a C2440 error. It says: 'static_cast':void* cannot be converted in wqueue<T>. I tried different cast ways but I always get the same error. As far as I found out I should get the error if I try to dereference without cast but in my case I cast before and still get that error.

void *srumbler (void *arg) {
wqueue<workclas*> m_queue= static_cast<wqueue<workclass*>>(arg);
return NULL;
}

The according type wqueue in the header file:

template <typename T> class wqueue {
list<T> m_queue;
pthread_mutex_t m_mutex;
pthread_cond_t m_condv;

[Code] .....

View 3 Replies View Related

C++ :: Add Variable Address To Void Pointer Inside Of Class?

Dec 1, 2013

How can I add the variable adress to a void pointer inside of a class?

class variant2 {
private:
void *Vvariant=NULL;
public:
template<typename b>
variant & operator = (b *adress)

[Code] ....

if possible i want avoid the '&' when i assign the variable address.(variant2 f=varname;//like you see i don't use the '&')
for the moment i just need put the address to Variant pointer. but i receive several errors .

View 4 Replies View Related

C++ ::  Why Every First Function Of Each File Get Error - Multiple Definition Of Void Pointer

May 6, 2014

I declared all functions in header file, such as:

bool readCase();

bool meshing();
bool readMesh();

bool calculateFlowfield();
bool readFlowfield();

bool calculateEvaporation();

And then I define them in separated .cpp files, each .cpp file include the header, but I got multiple definition error, why?

Even the int main() function, which only decalred and defined once got this error, why?

View 14 Replies View Related

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 View Related

C++ :: Possible To Change A Single Variable From A Pointer?

Dec 2, 2013

I am stuck at a problem where I have two pointers pointing to the same object, and I need to change an int on one of the pointers but point to the same object.

To be more specific, there is an array of Item objects. A long list of items a player can buy. Then, there is the player's inventory, a vector pointer. Whenever a player buys an item, it sets the pointer to the bought object.

The problem arises when he buys two of the same object. I tried to identify the objects with an ID, but it does nothing, because they are just pointing to the same object, and so I have no way of telling them apart.

This is further complicated by the fact that it is a polymorphic object. So, I can't simply make a new every time I buy an object, without making a hassle. Well, at least I am not familiar with that kind of code just yet.

View 18 Replies View Related

C++ :: Change From Array To Pointer Arithmetic?

Sep 12, 2013

#include <iostream>
#include <string>
using namespace std;

[Code]....

View 3 Replies View Related

C :: Change Arrays In Function Int Palin To Pointer

Oct 15, 2014

I'm trying to write a program to test if a string is palindromic using only pointers

Code:

#include <stdio.h>
#include <string.h>
void revstr(char* str)
}

[code]....

I need to change the arrays in the function int palin to pointers.

View 4 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 :: Linked List - Change Struct Pointers

Dec 17, 2013

I have a linked list where each node contains a pointer to a string, I'm treating the linked list as a text file and each node/string contains each line in the text file.

I have all of that working, but now I need to create a switchLine function that takes in the two line numbers and I need to switch those lines in the text file. I initially thought of switching the pointers to each the next node in the linked list such that the node order changes, but I then realized how much work that would actually take when you need to consider so many cases. So I instead thought of changing the pointers to strings. If I want to switch the 2nd and 3rd line, then I'd want to have the 2nd node in the list have its char * pointer point to the memory position that the 3rd node's char pointer points to, and vice versa.

View 2 Replies View Related

C :: Change / Fill List Without Changing Function Type

Feb 13, 2013

I am an IT student currently learning linked list. I have a problem with my code here. After I call addFront() my list doesn't change when I display it. How do I somewhat change/fill my list without changing the function type? I know it works on pointers still messed up with linked list.

Code:

typedef struct node *nodeptr;
struct node{int item;
nodeptr next;};
typedef nodeptr List;
}

[code]....

View 3 Replies View Related







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