473,508 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

skp6.c: In function ‘initList’: skp6.c:48: error: expected expression before ‘)’ toke

1 New Member
here is my complete code, can any one help me why does this error occurs and how to resolve it.

also, error is at the line "list.hdr =(list *) malloc(sizeof(struct SkipList));" is the size allocation here is correct or it should be diffrent.

code:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define compLT(a,b) (a < b)
  4. #define compEQ(a,b) (a == b)
  5. #define MAXLEVEL 15
  6. // levels range from (0 .. MAXLEVEL)
  7. struct Node_ {
  8.     int data;
  9.     struct Node_ *forward[1];
  10. } Node;
  11. struct SkipList{
  12.  struct Node_ *hdr;                  // list Header
  13.   int listLevel;              // current level of list
  14. }list;
  15.  
  16. <snipped>
  17.  
  18. //  initialize skip list
  19.    int initList() 
  20.   {
  21.     int i; 
  22.     list.hdr =(list *) malloc(sizeof(struct SkipList));
  23.     for (i = 0; i <= MAXLEVEL; i++)
  24.     list.hdr->forward[i] = list.hdr;
  25.     list.listLevel = 0;
  26.     printf("\nskip initialized");
  27.     printf("\ninitial current level of list=%d",list.listLevel);
  28.     printf("\ninitial skip is empty and all hdr pointer=list.hdr");
  29.   }
  30.  
thank you !
Sep 26 '09 #1
1 2884
Banfa
9,065 Recognized Expert Moderator Expert
list is a variable of type struct SkipList.

On the line in question you try to use it to cast the output of malloc.

In fact you do this

Reserve memory sized to the struct SkipList
Attempt to cast it to the variable "list *" this is a complete syntax error
Allocate the result to a pointer of type Node_ *

This is a completely confused attempt at allocation.

For some type T then you should perform a simple allocation like this (in C)

Expand|Select|Wrap|Line Numbers
  1. T *ptrData;
  2. ptrData = malloc(sizeof *ptrData);
  3.  
Some people use the alternative
Expand|Select|Wrap|Line Numbers
  1. T *ptrData;
  2. ptrData = malloc(sizeof(T));
  3.  
I prefer the first form because if for some reason you change the type of ptrData you do not have to change all your allocations.

In C++ you have to cast the output of malloc so it becomes
Expand|Select|Wrap|Line Numbers
  1. T *ptrData;
  2. ptrData = (T *)malloc(sizeof(T));
  3.  
but then you should be using new in C++.
Sep 27 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
4935
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
13
10645
by: john.constantine | last post by:
Hi I have this code: template<typename ClassType> struct S { template<typename FunctionType> void member() {}; };
4
2045
by: Aaron Walker | last post by:
Greetings, I'm attempting to write my first *real* template function that also deals with a map of strings to member function pointers that is making the syntax a little tricky to get right. ...
2
4559
by: Matthew Louden | last post by:
When I pass an array as a function parameter, it yields the following compile error. Any ideas?? However, if I create a variable that holds an array, and pass that variable to the function...
0
1530
by: Kent P. Iler | last post by:
Hi, I have a data repeater that is returning a list of events. One of the things I want to do is give the user a way to edit or delete an event. My plan was to use an Imagebutton that would...
6
9881
by: Peter Rothenbuecher | last post by:
Hello, when I try to compile the following code with g++ -o client client.c #include<sys/socket.h> #include<stdio.h> #include<stdlib.h> #define ADDRESS "mysocket"; #define MAXLEN 200;
42
3083
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
2
2461
by: utab | last post by:
Dear all, I tried sth easy(actually this was an exercise) but I tried to use the standard lib. heavily for this problem(as far as I can). There was one point I could not figure out. The problem...
5
3862
by: gdarian216 | last post by:
can I pass grades.projects in my function call that is void get_scores(ifstream& infile, int num_scores, grades.projects) and the function would look like void get_scores(ifstream& infile, int...
0
7224
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,...
0
7120
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...
0
7323
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,...
0
7380
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...
1
7039
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...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
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...

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.