GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-May-2007, 07:26
dlp dlp is offline
Member
 
Join Date: May 2006
Posts: 100
dlp will become famous soon enough

Re: double concatenated dynamic list - different type assignment warning


Does the warning give a line number or otherwise point to some specific place in your code?
  #2  
Old 31-May-2007, 07:34
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 114
ahbi82 will become famous soon enough

Re: double concatenated dynamic list - different type assignment warning


CPP / C++ / C Code:
if ((conductivity_cell->conductivity_values = malloc(sizeof(data_unit_t))) == NULL)
			fprintf(stderr, "Not enough memory");





cast it to
CPP / C++ / C Code:
(data_unit_t*)
when malloc
  #3  
Old 31-May-2007, 07:46
pisuke pisuke is offline
New Member
 
Join Date: May 2007
Posts: 19
pisuke is on a distinguished road

Re: double concatenated dynamic list - different type assignment warning


Hi!

dlp: the lines giving the warning are the ones I signaled with comments in the code.

ahbi82: when casting in malloc:

Code:
if ((current_unit_ptr->next = (data_unit_t *)malloc(sizeof(data_unit_t))) == NULL) // allocates fresh memory for a new element fprintf(stderr, "Not enough memory");

I get a new warning in the malloc line, and besides the other lines keep on warning.
  #4  
Old 31-May-2007, 08:43
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 452
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: double concatenated dynamic list - different type assignment warning


I tried putting together what you have posted but it is missing a lot of stuff.
I did see this though for starteres:
CPP / C++ / C Code:
/* given : */
typedef struct {
	float conductivity;
	double variance;
	unsigned int position;
	struct data_unit_t *prev;
	struct data_unit_t *next;
} data_unit_t;
/*
in the funtion prototype, conductivity_cell_t is not a 'data type'
data_unit_t * Add_Data_Unit (conductivity_cell_t * conductivity_cell, float cond) 
...looks like do you know that 'data_unit_t' is, so try: */
data_unit_t * Add_Data_Unit (data_unit_t * conductivity_cell, float cond)

/* and later: */
    data_unit_t *current_unit_ptr;   
    current_unit_ptr = conductivity_cell->conductivity_values;
/* this assignment doesn't look correct...
    matter of fact, is there a 'data_unit_t' declared yet?
    I think I see pointers but no named structure to even point at....
    I could very well be wrong */
Might I suggest you separate the data declarations and function out to a simple program with a simple main() to call it.
Get it running as well as you can and post the entire thing back (in C++ (not 'code') tags) ...so we can see EXACTLY what you are seeing...
Howard;

PS: good to have gidforums back on line, looked like a pretty good meltdown...
I note, however, that these posts are not in sequence in my browser...
You gettin that too?
  #5  
Old 31-May-2007, 08:43
pisuke pisuke is offline
New Member
 
Join Date: May 2007
Posts: 19
pisuke is on a distinguished road

Re: double concatenated dynamic list - different type assignment warning


Hi again,

I haven't found the solution yet, but I discovered another problem that perhaps is related, this time the compiler triggers a compilation error not a warning.

I'm writing a function to release the previous allocated memory for the dynamic list, here is the problem:
CPP / C++ / C Code:
void Free_Allocated_Memory(data_unit_t * head) {

	data_unit_t *ptr, *ptr1;
	
	ptr = head->next; // yields: warning: assignment from incompatible pointer type
	
	ptr1 = ptr->next->next; // yields: error: dereferencing pointer to incomplete type
}

I think that the second error is related with the warning, the warning can be solved with a cast, but how to solve the second one??

Any ideas?

Thanks
  #6  
Old 31-May-2007, 09:05
pisuke pisuke is offline
New Member
 
Join Date: May 2007
Posts: 19
pisuke is on a distinguished road

Re: double concatenated dynamic list - different type assignment warning


Hi Howard,

I didn't post the other data type: conductivity_cell_t, as I thought it was not related with the warning. Here is it:

CPP / C++ / C Code:
typedef struct {
	
	data_unit_t	*conductivity_values;
	....					
	double 		solute_mass;
	
} conductivity_cell_t;

data_unit_t *conductivity_values, is the head of a concatenated list, therefore I control everything in the program from the struct conductivity_cell_t (kind of parent struct), as there I have among other stuff the access point to the dynamic list.

The function "Add_Data_Element" accepts as an argument a pointer to the conductivity_cell_t struct, then I acceed the beginning of the list and I work with it.

here is a simplification of the code:

CPP / C++ / C Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

typedef struct {
	
	float conductivity;
	double variance;
	unsigned int position;
	struct data_unit_t *prev;
	struct data_unit_t *next;
	
} data_unit_t; 


typedef struct {
	
	data_unit_t	*conductivity_values;
	
} conductivity_cell_t;


int main(int argc, char *argv[])
{
	// variable definition 	
	static conductivity_cell_t *inlet;
	float inlet_cond;
	
	// inits conductivity cells related variables	
	inlet = Init_Conductivity_Cell();
	
	while (1) {
		printf("inlet value:");
		fflush(stdout);
		scanf("%f", &inlet_cond);

		if (inlet_cond == 0) break;
		else Process_New_Value(inlet,inlet_cond);

	} // end while		


	
	// frees memory
	Free_Allocated_Memory(inlet->conductivity_values);
	
	// return error code after completion 
	return 0;

}  

/* -------------------------- START FUNCTIONS -------------------------- */

conductivity_cell_t * Init_Conductivity_Cell () {
	
	conductivity_cell_t *conductivity_cell;
	
	if ((conductivity_cell = malloc(sizeof(conductivity_cell_t))) == NULL)
		fprintf(stderr, "Not enough memory");
	
	conductivity_cell->conductivity_values = NULL;
	
	return conductivity_cell;
	
	
}	// end Init_Conductivity_Cell


void Process_New_Value (conductivity_cell_t * conductivity_cell, float cond) {
	
	// Variable declaration
	
	data_unit_t *current_unit_ptr;
	
	
	// Ads new value to the conductivities dynamic list
	
	current_unit_ptr = Add_Data_Unit(conductivity_cell, cond);
		
	
}	// end Process_New_Value

data_unit_t * Add_Data_Unit (conductivity_cell_t * conductivity_cell, float cond) {


	// pointers to manage dynamic list of conductivity and variance values:
	data_unit_t *current_unit_ptr;	// points current data unit
	data_unit_t *prev_unit_ptr;		// points previous data unit


	/* The head of the dynamic list is accessed from the component data_values of
	 * the measure_unit_t struct. If it is undefined (NULL), means that the first value (head)
	 * of the list should be recorded, if not it appends values at the end of the list.
	 * It's a double concatenated list, thus each data_unit_t has two pointers: one to the previous
	 * value and a one to the next one. */

	if (conductivity_cell->conductivity_values == NULL) {

		if ((conductivity_cell->conductivity_values = malloc(sizeof(data_unit_t))) == NULL)
			fprintf(stderr, "Not enough memory");

		current_unit_ptr = conductivity_cell->conductivity_values;		
		current_unit_ptr->conductivity = cond;
		current_unit_ptr->variance = 0;
		current_unit_ptr->position = 1;
		current_unit_ptr->prev = NULL;
		current_unit_ptr->next = NULL;

	} else {

		current_unit_ptr = conductivity_cell->conductivity_values;

		while (current_unit_ptr->next != NULL) current_unit_ptr = current_unit_ptr->next;		// identifies the end of the list

		if ((current_unit_ptr->next = malloc(sizeof(data_unit_t))) == NULL)						// allocates fresh memory for a new element
			fprintf(stderr, "Not enough memory");

		prev_unit_ptr = current_unit_ptr;					// stores the current unit as the previous one
		current_unit_ptr = current_unit_ptr->next;			// sets the current pointer to the just allocated memory address
		current_unit_ptr->conductivity = cond;				// fills in the data
		current_unit_ptr->position = prev_unit_ptr->position+1;				
		current_unit_ptr->prev = prev_unit_ptr;
		current_unit_ptr->next = NULL;		

	}	// end if cell->conductivity_values
	
	return current_unit_ptr;

} 	// end Add_Data_Unit
  #7  
Old 31-May-2007, 10:09
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 452
Howard_L has a spectacular aura aboutHoward_L has a spectacular aura about

Re: double concatenated dynamic list - different type assignment warning


Sheez... I'll have to go now and won't return until late tonight. sorry
Maybe someone else can help in the meantime.
I tried your latest code and got lots of warnings. It needs work fer sher.
I would start building it back up from the beginning.
Start with a simple main() and data declarations including the structures.
NO functions or calls at this point.
Make some temporary things in main() to look at and manipulate your data.
Get comfortable with that and ensure that everything behaves as you expect.
Add in your first function and prototype in header.
If it gives problems reduce it to a minumum and add stuff one at a time.
Make sure what you pass to it and back is what you expect. (major problem causer)
Repeat....
Linked lists can be pretty confusing, but building one thing at a time helps me understand what is going on as I'm going on building...
Here are the majors of those warnings:
CPP / C++ / C Code:
D:\C\gidforum\pisuke>gcc -Wall -W -pedantic pisuke2.c -o pisuke2.exe
pisuke2.c: In function `main':
pisuke2.c:31: warning: implicit declaration of function `Init_Conductivity_Cell'
pisuke2.c:31: warning: assignment makes pointer from integer without a cast
pisuke2.c:39: warning: implicit declaration of function `Process_New_Value'
pisuke2.c:46: warning: implicit declaration of function Free_Allocated_Memory'
pisuke2.c: At top level:
pisuke2.c:55: warning: type mismatch with previous implicit declaration
pisuke2.c:31: warning: previous implicit declaration of `Init_Conductivity_Cell'
pisuke2.c:55: warning: `Init_Conductivity_Cell' was previously implicitly declared to return `int'
pisuke2.c:70: warning: type mismatch with previous implicit declaration
pisuke2.c:39: warning: previous implicit declaration of `Process_New_Value'
pisuke2.c:70: warning: `Process_New_Value' was previously implicitly declared to return `int'
pisuke2.c: In function `Process_New_Value':
pisuke2.c:79: warning: implicit declaration of function `Add_Data_Unit'
pisuke2.c:79: warning: assignment makes pointer from integer without a cast
pisuke2.c: At top level:
pisuke2.c:84: warning: type mismatch with previous implicit declaration
pisuke2.c:79: warning: previous implicit declaration of `Add_Data_Unit'
pisuke2.c:84: warning: `Add_Data_Unit' was previously implicitly declared to return `int'
pisuke2.c: In function `Add_Data_Unit':
pisuke2.c:114: warning: assignment from incompatible pointer type
pisuke2.c:120: warning: assignment from incompatible pointer type
pisuke2.c:123: warning: assignment from incompatible pointer type
C:\WINDOWS\TEMP/cciJm5fb.o(.text+0x98):pisuke2.c: undefined reference to `Free_Allocated_Memory'
Ususally attention to the errors at the beginning will reduce the errors below,
so recompile often , and put error reporting on full blast
Good Luck! (one step at a time...)
Howard;
  #8  
Old 31-May-2007, 12:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: double concatenated dynamic list - different type assignment warning


Quote:
Originally Posted by pisuke
Any ideas??

Change the typedef to something like:

CPP / C++ / C Code:
typedef struct data_unit_t{
    float conductivity;
    double variance;
    unsigned int position;
    struct data_unit_t *prev;
    struct data_unit_t *next;
} data_unit_t;


1. If it is a C program (not C++) and if you have included <stdlib.h> no casts are required anywhere. If you need a cast in this function, then you are doing something wrong somewhere else.

2. If it is a C++ program, you will need casts on the malloc() stuff, but I respectfully suggest that C++ programs use new and delete rather than malloc() and free()

3. I also respectfully suggest that you never cast the result of an expression on the left-hand side. Current versions of GNU compilers (4.something) have changed "deprecated" to "prohibited". (It wasn't part of the C standard, but for some god-awful reason, previous versions of the compilers allowed it. It's never necessary, and it's just downright "wrong".)


Regards,

Dave
  #9  
Old 31-May-2007, 13:42
pisuke pisuke is offline
New Member
 
Join Date: May 2007
Posts: 19
pisuke is on a distinguished road

double concatenated dynamic list - different type assignment warning


Hi there,

I am doing a concatenated dynamic list using structs as each data unit, and pointers to structs to access the previous and the next data unit, here is the struct definition:

CPP / C++ / C Code:
typedef struct {
	float conductivity;
	double variance;
	unsigned int position;
	struct data_unit_t *prev;
	struct data_unit_t *next;
} data_unit_t; 

Everything works fine, however I can’t get rid of a warning message telling:
“Warning: Assignment from incompatible pointer type”.

I guess that the reason is that I assign a pointer to null to a pointer to data_unit_t, which are actually different types. If I cast before the assignment I get rid of the warnings, however in one of the lines I get a new warning telling me that “leftside cast is deprecated”.

See the comments in the code:
CPP / C++ / C Code:
data_unit_t * Add_Data_Unit (conductivity_cell_t * conductivity_cell, float cond) { 


	// pointers to manage dynamic list 
	data_unit_t *current_unit_ptr;	// points current data unit
	data_unit_t *prev_unit_ptr;		// points previous data unit



	/* conductivitiy_cell_t is the parent struct, one of its components is a pointer to the
	 * first data_unit_t and therefore the head of the dynamic list */
	if (conductivity_cell->conductivity_values == NULL) {

		if ((conductivity_cell->conductivity_values = malloc(sizeof(data_unit_t))) == NULL)
			fprintf(stderr, "Not enough memory");

		current_unit_ptr = conductivity_cell->conductivity_values;		
		current_unit_ptr->conductivity = cond;
		current_unit_ptr->variance = 0;
		current_unit_ptr->position = 1;
		current_unit_ptr->prev = NULL;
		current_unit_ptr->next = NULL;

	} else {

		current_unit_ptr = conductivity_cell->conductivity_values;

		/* next line identifies the end of the list, and gives an assignemnt warning, 
		 * I can get rid of it using a cast:
		 * current_unit_ptr = (data_unit_t *)current_unit_ptr->next; */
		while (current_unit_ptr->next != NULL) current_unit_ptr = current_unit_ptr->next;		

		if ((current_unit_ptr->next = malloc(sizeof(data_unit_t))) == NULL)						// allocates fresh memory for a new element
			fprintf(stderr, "Not enough memory");

		prev_unit_ptr = current_unit_ptr;					// stores the current unit as the previous one
		
		/* following line gives assignment warning, not if I cast:
		 * current_unit_ptr = (data_unit_t *)current_unit_ptr->next; */
		current_unit_ptr = current_unit_ptr->next;			// sets the current pointer to the just allocated memory address
		current_unit_ptr->conductivity = cond;				// fills in the data
		current_unit_ptr->position = prev_unit_ptr->position+1;				
		
		/* again assignment warning, if I cast I got "left side cast deprecated":
		 * (data_unit_t *)current_unit_ptr = (data_unit_t *)current_unit_ptr->next; */		
		current_unit_ptr->prev = prev_unit_ptr;
		current_unit_ptr->next = NULL;		

	}	// end if cell->conductivity_values
	
	return current_unit_ptr;

} 	// end Add_Data_Unit


Any ideas??
Last edited by LuciWiz : 31-May-2007 at 11:56. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #10  
Old 31-May-2007, 15:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: double concatenated dynamic list - different type assignment warning


Quote:
Originally Posted by pisuke
Any ideas
Somehow the timestamps got scrambled, and my response appears before the post that I was responding to. Check Number 8 in this thread (a couple back from this one, I think).

Bottom line: if you have to use casts of any kind in the function you showed, then you need to do something else. (Assuming it's C and not C++).

Regards,

Dave
 
 

Recent GIDBlogWelcome to Baghdad by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 12:31
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 13:12
linked list error message Krandygrl00 C++ Forum 4 22-Jun-2005 14:13
Double output leanieleanz C++ Forum 1 11-Mar-2005 20:19

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:46.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.