Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# "local variable".. "used without having been initialized"?

Posted on 2003-11-13
Medium Priority
4,521 Views
I am getting an error ...node0.c(33) : warning C4700: local variable 'node0' used without having been initialized
but I thought I did initialize it?  Part of my code is shown below...
____________________________________________________________________________________________

#include "prog3.h"
#include <stdio.h>

extern struct rtpkt {
int sourceid;       /* id of sending router sending this pkt */
int destid;         /* id of router to which pkt being sent
(must be an immediate neighbor) */
int mincost[4];    /* min cost to node 0 ... 3 */
};

extern int TRACE;
extern int YES;
extern int NO;

struct distance_table
{
int costs[4][4];
} dt0;

printdt0(distance_table);
/* students to write the following two routines, and maybe some others */
/************************************************************************************************************************************/
/* rtinit0() -- This routine will be called once at the beginning of the emulation. rtinit0() has no arguments. */
void rtinit0()
{
struct rtpkt *pkt2send;
struct distance_table *node0;
int i, j;
/* default the distance table by setting the cost to infinity (999)). */

for (i=0; i<=3; i++){
for (j=0; j<=3; ++j)
node0->costs[i][j] = 999;
}

/* Initialize the distance table reflect the direct costs */
node0->costs[0][0] = 0;
node0->costs[1][1] = 1;
node0->costs[2][2] = 3;
node0->costs[3][3] = 7;

/* Finding the mincost from the distance table */
for (i=0; i<=3; i++){
pkt2send->mincost[i] = 999; // default mincost
}
for (i=0; i<=3; i++){
for (j=0; j<=3; ++j){
if (node0->costs[i][j]<pkt2send->mincost[i]){
pkt2send->mincost[i] = node0->costs[i][j];
}
}
}

/* Then send its distance vector directly connected neighbors the cost of it minimum cost paths to all other network nodes. */
pkt2send->sourceid = 0;
pkt2send->destid = 1;
tolayer2(pkt2send);
pkt2send->destid = 2;
tolayer2(pkt2send);
pkt2send->destid = 3;
tolayer2(pkt2send);

printdt0(node0);
}

/************************************************************************************************************************************/
/* rtupdate0(struct rtpkt *rcvdpkt) -- This routine will be called when node 0 receives a routing packet (distance vector) from its
directly connected neighbors.  */

void rtupdate0(rcvdpkt)
struct rtpkt *rcvdpkt;
{
int k, i, source;
struct rtpkt *pkt2send;
struct distance_table *node0;

/* the received distance vectors are used to update its own distance table (as specified by the distance vector algorithm). */
source = rcvdpkt->sourceid;
for (i=0; i<=3; i++){
node0->costs[i][source] = node0->costs[i][source] + rcvdpkt->mincost[i];
}

/* Finding it's minimum cost paths to all other neighboring nodes
(the distance vector). */
k = 0; //check to see if the distance vector gets updated
for (i=0; i<=3; i++){
if (pkt2send->mincost[0]<node0->costs[0][i]) {
pkt2send->mincost[0]=node0->costs[0][i];
k = 1;
}
if (pkt2send->mincost[1]<node0->costs[1][i]) {
pkt2send->mincost[1]=node0->costs[1][i];
k = 1;
}
if (pkt2send->mincost[2]<node0->costs[2][i]) {
pkt2send->mincost[2]=node0->costs[2][i];
k = 1;
}
if (pkt2send->mincost[3]<node0->costs[3][i]) {
pkt2send->mincost[3]=node0->costs[3][i];
k = 1;
}
}

/* If the distance vector is updated, then
This distance vector is sent to neighboring nodes in a routing packet by calling the routine tolayer2()*/
if ( k = 1) {
pkt2send->sourceid = 0;
pkt2send->destid = 1;
tolayer2(pkt2send);
pkt2send->destid = 2;
tolayer2(pkt2send);
pkt2send->destid = 3;
tolayer2(pkt2send);
}

printdt0(node0);
}

/************************************************************************************************************************************/
/* This routine will print the distance table for node 0 in a pretty way. It is passed a pointer to a structure of type distance_table.
printdt0() and the structure declaration for the node 0 distance table are declared in the file node0.c. Similar pretty-print
routines are defined for you in the files node1.c, node2.c node3.c. */

printdt0(dtptr)
struct distance_table *dtptr;

{
printf("                via     \n");
printf("   D0 |    1     2    3 \n");
printf("  ----|-----------------\n");
printf("     1|  %3d   %3d   %3d\n",dtptr->costs[1][1],
dtptr->costs[1][2],dtptr->costs[1][3]);
printf("dest 2|  %3d   %3d   %3d\n",dtptr->costs[2][1],
dtptr->costs[2][2],dtptr->costs[2][3]);
printf("     3|  %3d   %3d   %3d\n",dtptr->costs[3][1],
dtptr->costs[3][2],dtptr->costs[3][3]);
}

0
Question by:VWrestle97
[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

LVL 45

Expert Comment

ID: 9738020
struct distance_table *node0;
int i, j;
/* default the distance table by setting the cost to infinity (999)). */

for (i=0; i<=3; i++){
for (j=0; j<=3; ++j)
node0->costs[i][j] = 999;

when you declare node0, it has some junk value ... immediately after that, you use it in a loop !!!!

struct distance_table *node0 = NULL;

initialized ... but next you need to put some valid value in it or code will seg fault
0

LVL 45

Expert Comment

ID: 9738025
struct distance_table *node0;

/* the received distance vectors are used to update its own distance table (as specified by the distance vector algorithm). */
source = rcvdpkt->sourceid;
for (i=0; i<=3; i++){
node0->costs[i][source] = node0->costs[i][source] + rcvdpkt->mincost[i];
}

same here
0

LVL 16

Accepted Solution

_nn_ earned 1200 total points
ID: 9738037
node0 is possibly meant to point to dt0, so I guess it should be

struct distance_table *node0 = &dt0;

0

Author Comment

ID: 9738202
sunnycoder, I tried your suggestion but I don't think that it worked correctly... I have more code... perhaps ... if you can, you look at my full code... is there a way that I can send it you so that you can take a better look at it?
0

LVL 45

Expert Comment

ID: 9738216
you need to initialize it to a struct distance table .. if you have just one distance table (global one), then try _nn_ suggestion
declare node0 as
struct distance_table *node0 = &dt0;

in functions void rtinit0() and void rtupdate0(rcvdpkt)

if that does not work, post your code here ... if it is too big to be posted, my mail id is in my profile
0

LVL 5

Assisted Solution

g0rath earned 800 total points
ID: 9739383
but that is global so everyone can modify it's contents, if you are just wanting a local one just do this

struct distance_table node0;

then all your calls would be like

node0.costs[i][j]     // etc....

But if you are going with the pointer route then...

struct distance_table *node0 = NULL; // better practice

node0 = (struct distance_table *)malloc(sizeof(struct distrance_table));

then your logic works as you have it written...

assuming you are wanting a local version for local work...

But if you wanted a local COPY then you would add this line after allocating memory for it (pointer version)...

memcpy((void *)node0,&dt0,sizeof(struct distance_table));

0

## Featured Post

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
###### Suggested Courses
Course of the Month9 days, 3 hours left to enroll