Thread: Help with error

  1. #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;
      }
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.

  4. #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...

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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.)

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    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.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread