473,796 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structs help

Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

just to test in my main method I tried to initialize the
listNodePtr variable to null like this...

listNodePtr *head = NULL;

First as far as I understand listNode and *listNodePtr are
variables of type ln. However I have also learned, so I thought,
that listNodePtr is an alias to the struct ln. So my statement
above should be legal. Is It?

When I try to pass my new listNodePtr variable *head to my
listInsert method like........

listInsert(head ,1,2);

it doesn't work. It builds but at run time my program hangs
when inside the listInsert method the first if condition
tries to determine of the listNodePtr *list is null or not.
i.e. if( *list == NULL )..... this is where the program quits.

The method definition looks like...

void listInsert(list NodePtr *list, int key, int value)

I am assuming the body of the method is correct as this code
was given to us to use by my Prof.

Please help what am I doing wrong during my initialization?
How do I use structs defined like the one above?

Thanks in advance.
Nov 14 '05
22 2060


Barry Schwarz wrote:
On Sat, 03 Apr 2004 22:36:05 -0600, Dave Cooke
<dc****@ee.uman itoba.ca> wrote:

Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

just to test in my main method I tried to initialize the

C does not have methods. It has functions.

listNodePtr variable to null like this...

listNodePtr *head = NULL;
I suspect the OP wants:
listNodePtr head = NULL;

When I try to pass my new listNodePtr variable *head to my
listInsert method like........

listInsert(he ad,1,2);
The call:
listInsert(&hea d,1,2);

The method definition looks like...

void listInsert(list NodePtr *list, int key, int value)

Since C passes by value, how do you expect the results of this
function to be available to the calling function. Anything you do to
list will disappear as soon as listInsert returns.


Actually this prototype looks correct. It can modify a variable
in the calling function.
For example in the following, the value of head will be modified
should the function listInsert successfully allocate storage.

#include <stdlib.h>
#include <stdio.h>

typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

void listInsert( listNodePtr *p, int key, int data);

int main(void)
{
listNodePtr head = NULL;

printf("In Main: Before call to listInsert function head"
" is %sNULL\n", head?"not ":"");
listInsert(&hea d,1,31);
printf("In Main: After call to listInsert function head"
" is %sNULL\n", head?"not ":"");
free(head);
return 0;
}
void listInsert( listNodePtr *p, int key, int data)
{
listNodePtr new;

new = malloc(sizeof *new);
if(new != NULL)
{
new->key = key;
new->data = data;
new->next = *p;
*p = new;
}
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #21
On Sun, 04 Apr 2004 21:00:09 -0400, Al Bowers <xa******@rapid sys.com>
wrote:
Barry Schwarz wrote:
On Sat, 03 Apr 2004 22:36:05 -0600, Dave Cooke
<dc****@ee.uman itoba.ca> wrote:

Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

just to test in my main method I tried to initialize the

C does not have methods. It has functions.

listNodePt r variable to null like this...

listNodePt r *head = NULL;
I suspect the OP wants:
listNodePtr head = NULL;

When I try to pass my new listNodePtr variable *head to my
listInsert method like........

listInsert(h ead,1,2);
The call:
listInsert(&he ad,1,2);

The method definition looks like...

void listInsert(list NodePtr *list, int key, int value)

Since C passes by value, how do you expect the results of this
function to be available to the calling function. Anything you do to
list will disappear as soon as listInsert returns.


Actually this prototype looks correct. It can modify a variable
in the calling function.


Of course it can but only if the argument is of the form &variable or
equivalent. The OP's argument wasn't and therefore it couldn't. All
of which was covered in the subsequent paragraphs of my response which
you chose to omit.
<<Remove the del for email>>
Nov 14 '05 #22
On Sun, 04 Apr 2004 21:00:09 -0400, Al Bowers <xa******@rapid sys.com>
wrote:
Barry Schwarz wrote:
On Sat, 03 Apr 2004 22:36:05 -0600, Dave Cooke
<dc****@ee.uman itoba.ca> wrote:

Hi I am very new to C. I am trying to figure out how to initialize
a struct in my main program.
The struct is declared in anouther header file like this...
typedef struct ln {
int key;
int data;
struct ln *next;
} listNode, *listNodePtr;

just to test in my main method I tried to initialize the

C does not have methods. It has functions.

listNodePt r variable to null like this...

listNodePt r *head = NULL;
I suspect the OP wants:
listNodePtr head = NULL;

When I try to pass my new listNodePtr variable *head to my
listInsert method like........

listInsert(h ead,1,2);
The call:
listInsert(&he ad,1,2);

The method definition looks like...

void listInsert(list NodePtr *list, int key, int value)

Since C passes by value, how do you expect the results of this
function to be available to the calling function. Anything you do to
list will disappear as soon as listInsert returns.


Actually this prototype looks correct. It can modify a variable
in the calling function.


Of course it can but only if the argument is of the form &variable or
equivalent. The OP's argument wasn't and therefore it couldn't. All
of which was covered in the subsequent paragraphs of my response which
you chose to omit.
<<Remove the del for email>>
Nov 14 '05 #23

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3406
by: Roman Hartmann | last post by:
hello, I do have a question regarding structs. I have a struct (profil) which has a pointer to another struct (point). The struct profil stores the coordinates of points. The problem is that I don't know how many points there will be in every struct in the end, so I have to allocate memory dynamically for them and can't use an array of fixed size, unfortunately. I would like to know if there is a better way to access struct members...
10
2078
by: Angel | last post by:
I'm using several C functions (in a dll) that receive a struct as parameter. Since I'm doing it in C#, I assume I need to recreate the struct in C# in order to call the function with the required parameter. What would I need to do in order to convert a struct that looks like this: typedef struct { char rsvd0; char iadl1;
6
3919
by: marabo82 | last post by:
Hi, i know that typedef structs creates an aliases for a particular struct in C++. i m trying to convert .h c++ files to a namespace in C#.net so that i use C++ DLL in my C# application. one of the structs in C++ is something like typedef struct mystruct {....} mystruct1, *mystruct2; since typedef is used, mystruct1 and *mystruct2 are aliases to mystruct. how can i acheive the same (i.e aliasses in c#)? i can subtitute mystruct1 and...
3
1420
by: marabo82 | last post by:
Hi, i know that typedef structs creates an aliases for a particular struct in C++. i m trying to convert .h c++ files to a namespace in C#.net so that i use C++ DLL in my C# application. one of the structs in C++ is something like typedef struct mystruct {....} mystruct1, *mystruct2; since typedef is used, mystruct1 and *mystruct2 are aliases to mystruct. how can i acheive the same (i.e aliasses in c#)? i can subtitute mystruct1 and...
5
2640
by: Steve Edwards | last post by:
Hi, With much help from this forum I've been using stl containers, but always with other common - or stl - types as members. I've now run in to a problem while trying to use some of my own structures: enum MySym{symA, symB, symC, symD ... etc.} typedef struct{ MySym s1, s2, s3;
61
3783
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch (myBranch + x) = new Branch(i); // it doesn't x is a loop iterator, i is an int for the constructor to define an array. What am I doing wrong here.
4
1771
by: engggirl3000 | last post by:
I was told to write a function that will read data, in a user defined text file, into a vector of structs. I am not sure where to start with this. Could some one tell me what is a vector of structs, I know vectors and I know structs, but I dont know whats a vector of structs. As well, is there a specific algoritm in c++ to use when reading from a user defined text file, or does cin work? If someone can give me a few pointers to help get...
29
2792
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much overhead (I think). A Struct seems more appropriate. At least it is what I would have used in other languages. But since a Struct *can* hold methods, I wander if I am saving anything. If not, why use it?
1
4771
by: radskate360 | last post by:
Hi I am newer to programming and need a bit of help with this program. OK, heres the directions. The distance between two places on earth can be calculated by using their latitudes and longitudes. The calculation for this is as follows: (The latitudes and longitudes must be converted to radians (radians=degrees * pi / 180)). PI must be set set to 20 decimals as follows: PI = 3.1419265358979323846 Earth's Radius = 3963.1
2
4359
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of structs. The array of structs are allocated by C#. If I pass the array without 'ref' it works fine on the C++ side, but after returning to C# the array struct values are all zero. If I pass with 'ref' the application crashes. If I use a class...
0
9680
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9052
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5442
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.