Newb to C, have a few questions + code.

Started by
8 comments, last by Pipes McGee 17 years, 7 months ago
I was using this (all the code is on that page) tutorial and ran into a bit of a pickle. You see (I'm using Ubuntu [XP Home on dual-boot]), when I try to compile util.c I get this error message:

util.c: In function ‘bubble_sort’:
util.c:4: error: parameter ‘rand_seed’ is initialized
util.c:9: error: syntax error before ‘{’ token
Any advice? I have it exactly as shown in the tutorial. Also, do any of you have any better C (not C++) resources? I've been having some fun with this one, but I guess I can't rely on buggy code, no? Much thanks in advance. :)
Advertisement
At a glance, the code on the page looks okay.

Why don't you paste the code you actually have, perhaps you made a transcription error?
How are you compiling this?
Here's the code for util.c:
/* util.c */#include "util.h"int rand_seed=10;/* from K&R - produces a random number between 0 and 32767.*/int rand(){    rand_seed = rand_seed * 1103515245 +12345;    return (unsigned int)(rand_seed / 65536) % 32768;}void bubble_sort(int m,int a[]){	int x,y,t;     	for (x=0; x < m-1; x++)        	for (y=0; y < m-x-1; y++)            		if (a[y] > a[y+1])            		{                		t=a[y];                		a[y]=a[y+1];                		a[y+1]=t;            		}}
Well, I've decided to go with cprogramming.com instead. So, yeah.
... compiles fine with Visual Studio 2005. Dont have gcc around to try it that way.

Ok, so it must be something local to gcc. I really like the tutorial I was using too, I wonder what the problem might be.
It compiles fine with gcc as well. I suspect that you need to change compilers, not tutorials.

EDIT: Which version of gcc are you running?
Plus... I would suggest switching tutorials anyways. For something aimed at new developers that syntax is horrible. The variables names are terrible and the lack of curly braces on a pair of nested loops is unforgivable. Its a needless complexity to someone new to programming and for the most part is just lazy anyways.
gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)

I have no idea what could be causing that problem. Anyway, I found a book at my library called C Primer Plus 5th Edition (2005), so I'm going to give that a go. Hopefully it will be more in depth than the tutorial I was using.

This topic is closed to new replies.

Advertisement