Hi all
If this is the wrong list for beginners trouble I apologize.
please refer to me the correct list in such case.
I am trying to create a simple linked list but I keep getting segmentation
errors and I dont know why. I realize that I do not free the allocated
memory but the segfault happpens (it seems) with the insert function.
Can any one explain ?
Regards Lars
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
Â* struct node * next;
Â* int data;
} Node;
Node * alloc_node(int n);
void insert(Node * head, int n);
int main(){
Â* Node * nod = NULL;
Â* insert(nod,4);
Â* printf("%d\n",nod->data);
Â* printf("%10p\n", nod);
Â* printf("%10p\n", nod->next);
Â* return 0;
}
Node * alloc_node(int n){
Â* Node * tmp;
Â* tmp = (Node *) malloc(sizeof(Node));
Â* tmp->data = n;
Â* tmp->next = NULL;
Â* return tmp;
}
void insert(Node * head, int n) {
Â* Node * tmp;
Â* if (head==NULL) {
Â* Â* head = alloc_node(n);
Â* }
Â* else { Â*
Â* Â* tmp = alloc_node(n);
Â* Â* head->next = tmp;
Â* }
} 6 1488
Lars wrote:
Hi all
If this is the wrong list for beginners trouble I apologize.
please refer to me the correct list in such case.
I am trying to create a simple linked list but I keep getting segmentation
errors and I dont know why. I realize that I do not free the allocated
memory but the segfault happpens (it seems) with the insert function.
Can any one explain ?
Regards Lars
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
struct node * next;
int data;
} Node;
Node * alloc_node(int n);
void insert(Node * head, int n);
int main(){
Node * nod = NULL;
insert(nod,4);
printf("%d\n",nod->data);
There's your problem, nod is NULL.
Node * alloc_node(int n){
Node * tmp;
tmp = (Node *) malloc(sizeof(Node));
You don't have to (and shouldn't) cast the return value of malloc.
tmp = malloc(sizeof *tmp);
--
Ian Collins.
Ian Collins wrote:
Lars wrote:
>Hi all If this is the wrong list for beginners trouble I apologize. please refer to me the correct list in such case.
I am trying to create a simple linked list but I keep getting segmentation errors and I dont know why. I realize that I do not free the allocated memory but the segfault happpens (it seems) with the insert function. Can any one explain ?
Regards Lars
#include <stdio.h> #include <stdlib.h>
typedef struct node { struct node * next; int data; } Node;
Node * alloc_node(int n);
void insert(Node * head, int n);
int main(){ Node * nod = NULL; insert(nod,4); printf("%d\n",nod->data);
There's your problem, nod is NULL.
>Node * alloc_node(int n){ Node * tmp; tmp = (Node *) malloc(sizeof(Node));
You don't have to (and shouldn't) cast the return value of malloc.
tmp = malloc(sizeof *tmp);
Thanks for your reply. But is this not handled by the the insert functio
through this ?
if (head==NULL) {
Â* Â* head = alloc_node(n);
Â* }
Regards
Lars
Lars wrote:
Ian Collins wrote:
>>> Node * alloc_node(int n);
void insert(Node * head, int n);
int main(){ Node * nod = NULL; insert(nod,4); printf("%d\n",nod->data);
There's your problem, nod is NULL.
Thanks for your reply. But is this not handled by the the insert functio
through this ?
if (head==NULL) {
head = alloc_node(n);
}
No, ant changes to head are local to the function, heat is a pointer, if
you wanted the change to be visible to the caller, it would have to be a
pointer to a pointer.
void insert(Node** head, int n);
--
Ian Collins.
Ian Collins wrote:
Lars wrote:
>Ian Collins wrote:
>>>> Node * alloc_node(int n);
void insert(Node * head, int n);
int main(){ Node * nod = NULL; insert(nod,4); printf("%d\n",nod->data); There's your problem, nod is NULL. Thanks for your reply. But is this not handled by the the insert functio through this ? if (head==NULL) { head = alloc_node(n); }
No, ant changes to head are local to the function, heat is a pointer, if
you wanted the change to be visible to the caller, it would have to be a
pointer to a pointer.
void insert(Node** head, int n);
Thats it!!!
I completely overlooked that.
Thank you very much.
Best regards
Lars
Richard Bos wrote:
Lars <a@nondisclosure.dkwrote:
>If this is the wrong list for beginners trouble I apologize.
This is not a list, it's a newsgroup. And if you post questions about
ISO C, not about, oh, I dunno, M$VC#++4.6 or Ganuck 4.77.35.88.55-alpha,
you should be fine here whether you're a beginner or not. Your question
is perfectly on-topic. However...
>#include <stdio.h> #include <stdlib.h>
typedef struct node { Â struct node * next; Â int data;
....you might want to check what is inserting these weird characters into
your code, and...
Your client? They don't appear to be in the message source.
--
Ian Collins.
On Mon, 11 Feb 2008 02:47:11 -0600, Ian Collins wrote
(in article <61*************@mid.individual.net>):
Richard Bos wrote:
>Lars <a@nondisclosure.dkwrote:
>>If this is the wrong list for beginners trouble I apologize.
This is not a list, it's a newsgroup. And if you post questions about ISO C, not about, oh, I dunno, M$VC#++4.6 or Ganuck 4.77.35.88.55-alpha, you should be fine here whether you're a beginner or not. Your question is perfectly on-topic. However...
>>#include <stdio.h> #include <stdlib.h>
typedef struct node { Â struct node * next; Â int data;
....you might want to check what is inserting these weird characters into your code, and...
Your client? They don't appear to be in the message source.
I saw them as well. I'm pretty sure it's some UTF/Unicode weirdness
with tab expansion or perhaps newline encoding somewhere along the way,
and we're both using different clients, on different operating systems.
Being that the OP is apparently in Denmark, it's a safe bet that it
something along those lines.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rolf Wester |
last post by:
Hi,
I'm just beginning PHP programming. I have installed Apache 2.0.48 and
PHP 4.3.3. The installation went well. Then I was trying a little example.
HTML page:
<html>
<head>...
|
by: P.Hill |
last post by:
I was just trying to install Apache and php on an XP box for test of a PHP
website I built last year. It was my first and only, so I am now rusty about
the little I did learn last year.
I get...
|
by: Will |
last post by:
Pardon two post in a row to the newsgroup but I want to try and expedite
this, if you guys don't mind helping out...
I running Windows XP Pro and wanted to download Python and any additional...
|
by: blah |
last post by:
I m actually a Novice in Python as well as Linux, When i look up
things on the internet about Linux Flavours, They are written so
complex that it is difficult for me to understand, i am asking if...
|
by: Eggnog |
last post by:
Hi,
Is there a newsgroups for beginners questions?
Cheers,
Nawg
|
by: William Foster |
last post by:
Does anyone know of a good online tutorial for C# focused on beginners. I
have been to many great sites like csharpfriends, csharp-corner etc looking
for good tutorials and have had no luck. Any...
|
by: aman firoz |
last post by:
do you people got anything for c beginners.....
help me out guys
|
by: Dual_b00t |
last post by:
hi i created a site called PHP Together its for beginners also for
gurus to help out the beginners
if you are learning PHP and feel alone then drop by .
ciao
Mark
|
by: yltkhuu |
last post by:
1. How does having a widely adopted C++ standard help game
programmers?
2. What are the advantages ans disadvantages of employing the "using"
directive?
3. Why might you define a new name for an...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |