C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 07-16-2009, 03:47 PM   #1
Registered User
 
Join Date: Jun 2009
Posts: 28
Help with error

When compiling I get these errors:
Code:
sort.c:21error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before âintâ
sort.c:46: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before â{â to          ken
I was thinking it had something to do with the way I passed my dynamically allocated array to my function, but i cant think of any other way to do it. ive tried using *a, and *a[]...any suggestions?


Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void quick_sort(int **a, int left, int right)
void swap(int **a, int i, int j)



int main (){
int number = 0;
printf("How many entries in the sort array?\n");
scanf( "%d", &number);


int *a = malloc(sizeof(int)*number);

	int j = 0;
	for( j = 0; j<number; j++){
		a[j] = random()%100;
		printf("%d\n", a[j]);
	}

	quick_sort(a, 0, number);
	int d;
	for(d=0; d<number;d++){
		printf("%d\n", a[d]);
	
	}
	
	
return 0;
}

void swap(int **a, int i, int j){
	int temp;
	
	temp = a[i];
	a[j] = a[i];
	a[j] = temp;

}

void insertion_sort(int **a, int n) {
  int k;
  for (k = 1; k < n; ++k) {
    int key = a[k];
    int i = k - 1;
    while ((i >= 0) && (key < a[i])) {
      a[i + 1] = a[i];
      --i;
    }
    a[i + 1] = key;
  }
}
bigparker is offline   Reply With Quote
Old 07-16-2009, 03:48 PM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Code:
void quick_sort(int **a, int left, int right)
void swap(int **a, int i, int j)
Forward declarations need a semicolon at the end;

EDIT: Also, why do those methods take a double pointer? It seems like you just need to pass the pointer here.

Last edited by bithub; 07-16-2009 at 03:51 PM.
bithub is offline   Reply With Quote
Old 07-16-2009, 03:51 PM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Which line is line 21 and which line is line 46? By my count, line 21 is "a[j] = random()%100;", which doesn't have an int token on it.
tabstop is offline   Reply With Quote
Old 07-16-2009, 03:59 PM   #4
Registered User
 
Join Date: Jun 2009
Posts: 28
Code:
line 21 is at "int main(){}" 
line 46 is "void swap(int *, int, int)"
and those are double pointers because I thought since my dynamic array was already a pointer, I needed a pointer to the array in the functions in which I call it...
bigparker is offline   Reply With Quote
Old 07-16-2009, 04:03 PM   #5
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
Quote:
and those are double pointers because I thought since my dynamic array was already a pointer, I needed a pointer to the array in the functions in which I call it...
No, you only need to pass a double pointer if you intend for the function to modify the pointer itself. If you are only modifying the data in the array, then just pass the pointer.
bithub is offline   Reply With Quote
Old 07-16-2009, 04:03 PM   #6
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
So yes your prototypes need semicolons.

And you only need to pass a pointer to your dynamic array if you intend to completely blow your dynamic array away and create a new one. (I.e., you need your pointer to point somewhere else.) If you're just going to move things around in the array you already have, then there's no need for an extra star. (Your dynamic array is an int *, so the ints that are pointed to can be changed by a function, but not the value of the pointer itself.)
tabstop is offline   Reply With Quote
Old 07-16-2009, 04:07 PM   #7
Registered User
 
Join Date: Jun 2009
Posts: 28
Quote:
No, you only need to pass a double pointer if you intend for the function to modify the pointer itself. If you are only modifying the data in the array, then just pass the pointer.
Quote:
So yes your prototypes need semicolons.

And you only need to pass a pointer to your dynamic array if you intend to completely blow your dynamic array away and create a new one. (I.e., you need your pointer to point somewhere else.) If you're just going to move things around in the array you already have, then there's no need for an extra star. (Your dynamic array is an int *, so the ints that are pointed to can be changed by a function, but not the value of the pointer itself.)
oh ok...i understand. thanks guys, that helped me out alot...its compiling fine now.
bigparker is offline   Reply With Quote
Reply

Tags
dynamic array, error

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting an error with OpenGL: collect2: ld returned 1 exit status Lorgon Jortle C++ Programming 6 05-08-2009 08:18 PM
An error is driving me nuts! ulillillia C Programming 5 04-04-2009 09:15 PM
Making C DLL using MSVC++ 2005 chico1st C Programming 26 05-28-2008 01:17 PM
Connecting to a mysql server and querying problem Diod C++ Programming 8 02-13-2006 10:33 AM
Couple C questions :) Divx C Programming 5 01-28-2003 01:10 AM


All times are GMT -6. The time now is 06:20 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22