c: segmentation fault (selection sort) - c

#include<stdio.h>
void selsort(int a[],int s)
{
int min,temp,i,j;
for(i = 0; i < s ; i++)
{
min = 0;
for(j = i+1 ; j < s; j++)
{
if(min > a[j])
{
min = j;
}
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
for (j=0;j<s;j++)
{
printf("%d",a[i]);
}
printf("\n");
}
}
main()
{
int i,a[5];
int size = 5;
printf("Enter elements of the array");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
selsort(a[5],size);
}
The error is as follows:
selsort.c:35:2: warning: passing argument 1 of ‘selsort’ makes pointer from integer without a cast [enabled by default]
selsort.c:2:1: note: expected ‘int *’ but argument is of type ‘int’
any tips on how to avoid this problem in the future will be really helpful

You should be calling your function like this:
selsort(a, size);
a[5] means "element at index 5" (past the end of your array, by the way: the largest legal element in int a[5] is at index 4).
You should also replace
min = 0;
with
min = i;
and use it like this:
if(a[min] > a[j]) ...

In the function call selsort(a[5], size); you are only passing the 5th [nonexistent] member of a. You want selsort(a, size);

Related

expected ‘int **’ but argument is of type ‘int (*)[3]’

I just started to tinker with pointers in multidimensional arrays and was trying to pass and array into a void function. The compilator just threw an error. I need the array to be passed exactly as a pointer to maybe change it then by reference.
#include <stdio.h>
#include <stdlib.h>
void Func(int** matrix, int sizeFirst, int sizeSecond)
{
for (int i = 0; i < sizeFirst; i++)
{
for (int j = 0; j < sizeSecond; j++)
{
printf("%d\n", matrix[i][j]);
}
}
}
int main()
{
int array[2][3] = {
{5,8,2},
{1,3,6}
};
int sizeMain = sizeof(array)/sizeof(array[0]);
int sizeInner = sizeof(array[0])/sizeof(array[0][0]);
Func(array, sizeMain, sizeInner);
return 0;
}
2D array is not pointer to pointer. Your code is invalid as Func does not know how many columns every row has. Also, use the correct type for sizes & indexes
void Func(size_t sizeFirst, size_t sizeSecond, int (*matrix)[sizeSecond])
{
for (int i = 0; i < sizeFirst; i++)
{
for (int j = 0; j < sizeSecond; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
int main()
{
int array[2][3] = {
{5,8,2},
{1,3,6}
};
size_t sizeMain = sizeof(array)/sizeof(array[0]);
size_t sizeInner = sizeof(array[0])/sizeof(array[0][0]);
Func(sizeMain, sizeInner, array);
return 0;
}
https://godbolt.org/z/Ejfrdd3nK
With pointers to VLAs, you can preserve all of the dimensions in the type of the argument and then let C do the multidimensional scaling for you.
In older versions of C you need to calculate the spot manually, remembering that
the first dimension from the right moves by 1 (scaled by sizeof(type), which C does for you), the second by first dimension from the right,
the third by first dimension from the right multiplied by second dimension from the right and so on.
It's sort of like digits in numbers (units, tens, hundreds, ...) except that the next scaling is the previous scaling multiplied by the previous dimension rather than by a constant radix.
(You could let C convert groups of decimal digits to decimal numbers by letting it subtract some &decdigits[a][b][c] and &decdigits[0][0][0] where decdigits is some decdigits[10][10][10], i.e.:
char decdigits[10][10][10]; printf("%d\n", (int)(&decdigits[1][2][3] - &decdigits[0][0][0])) /*prints 123*/;)
#include <stdio.h>
void Func( int sizeFirst, int sizeSecond, int (*matrix)[sizeFirst][sizeSecond])
{
for (int i = 0; i < sizeof((*matrix))/sizeof((*matrix)[0]) ; i++)
{
for (int j = 0; j < sizeof((*matrix)[0])/sizeof((*matrix)[0][0]) ; j++)
{
printf("%d ", (*matrix)[i][j]);
}
puts("");
}
}
void Func_89( int sizeFirst, int sizeSecond, int *matrix)
{
for (int i = 0; i < sizeFirst; i++)
{
for (int j = 0; j < sizeSecond; j++)
{
printf("%d ", matrix[i*sizeSecond + j]);
}
puts("");
}
}
int main()
{
int array[2][3] = {
{5,8,2},
{1,3,6}
};
int sizeMain = sizeof(array)/sizeof(array[0]);
int sizeInner = sizeof(array[0])/sizeof(array[0][0]);
Func(sizeMain, sizeInner,&array );
puts("===");
Func_89(sizeMain, sizeInner,&array[0][0] );
//char decdigits[10][10][10]; printf("%d\n", (int)(&decdigits[1][2][3] - &decdigits[0][0][0])); //123
return 0;
}
First of you have to understand what int ** represent, it represents pointer to integer pointer, here's an example:
int a = 10;
int *p = &a; // 'p' points to 'a`
int **pp = &p; // 'pp' points to 'p' (pointer to integer)
So because of this, you can't use int ** for an integer array. Now that's out of the way, let's see what can you do instead (solution)
You can simply add a pointer to an integer array in a function definition like this
void Func(int *matrix[], int rows, int cols)
Or you can simply do
void Func(int matrix[rows][cols])
Note: Array of strings or 2D character array (char array[][]) can be represented as char**

how to find out if a serie is sorted using pointers in c

i have the following program in c lanquage:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool is_sorted ( int *array , int num , int prev , int *index);
int main ()
{
int N;
int i;
int prev;
int data[100];
bool flag;
printf("Enter length:\n");
scanf("%d",&N);
printf("Enter %d integers:\n" ,N);
for (i =0; i<N; i++)
{
scanf("%d",&data[i]);
}
printf("Enter previous number:\n");
scanf("%d",&prev);
int *index= NULL;
flag = is_sorted(data,N,prev,index);
if ( !flag )
{
printf("%d ", *index);
}
}
bool is_sorted ( int *array , int num , int prev , int *index)
{
if ( prev > array[0] )
{
index=prev;
return false;
}
for ( int i=0; i<num; i++)
{
if ( array[i] > array[i+1] )
{
index = array[i];
return false;
}
}
return true;
}
The function is_sorted takes as input an array of integers and another one random integer and returns true if prev < array[0] < array[1] < ... < array[n].
I am using a pointer in order to find which is the first element to spoil the serie's order but i am a little bit confused with pointer's syntax.
Running it i am getting the following results:
pointers.c:43:14: warning: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
43 | index=prev;
| ^
pointers.c:51:19: warning: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
51 | index = array[i];
index is a pointer to an integer. You need to dereference the pointer to assign to the variable that it points to in the caller.
*index = prev;
...
*index = array[i];

Sorting array algorithm error

Hi I wrote this sorting algorithm and I'm not sure why I'm getting the following error: "member reference base type 'int' is not a structure or union"
void sort(float avg_dist, cg[]){
int i,j,t;
for(i=1; i<=cg[i]-1; i++)
for(j=1; j<=cg[i]-i; j++)
if(cg[j-1].avg_dist >= cg[j].avg_dist){
t = cg[j-1];
cg[j-1] = cg[j];
cg[j] = t;
}
}
cg is an int array.
You can't access a "member" of an int, as in
cg[j-1].avg_dist
I'm not sure what you're trying to do. Maybe multiply ?
cg[j-1] * avg_dist
It's not the problem, but you're (perhaps intentionally) omitting the type specifier in your function
void sort(float avg_dist, cg[]){
C is defaulting to an int array type, which of course, renders cg[j].avg_dist syntactically invalid. (In reality you probably want to multiply by avg_dist, use * rather than the member selection operator .).
This should give you a clear Idea:
#include <stdio.h>
void sortArray(int *array, int length){
int i,j, k, temp;
for (i = 0 ; i < length-1; i++){
for (k = 0 ; k < length-i-1; k++){
if (array[k] > array[k+1]){
temp = array[k];
array[k] = array[k+1];
array[k+1] = temp;
}
}
}
printf("The result:\n");
for ( j = 0 ; j < length ; j++ ){
printf("%d ", array[j]);
}
}
int main(void){
int array[] = {1,4,2,-1,2,3,4,1,3,-1};
int length = sizeof array / sizeof array[0];
sortArray(array, length);
printf("\n");
return 0;
}
Output:
The result:
-1 -1 1 1 2 2 3 3 4 4

How to return an array from a function with pointers

i'm trying to figure out how to return an array from a function in the main().
I'm using C language.
Here is my code.
#include <stdio.h>
int *initArray(int n){
int i;
int *array[n];
for(i = 0; i < n; i++){
array[i] = i*2;
}
return array;
}
main(){
int i, n = 5;
int *array[n];
array[n] = initArray(n);
printf("Here is the array: ");
for(i = 0; i < n; i++){
printf("%d ", array[i]);
}
printf("\n\n");
}
And this is the errors the console gives me:
2.c: In function ‘initArray’:
2.c:8:13: warning: assignment makes pointer from integer without a cast [enabled by default]
array[i] = i*2;
^
2.c:11:3: warning: return from incompatible pointer type [enabled by default]
return array;
^
2.c:11:3: warning: function returns address of local variable [-Wreturn-local-addr]
2.c: In function ‘main’:
2.c:23:4: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
printf("%d ", array[i]);
^
It's impossible!
I hate being a noob :(
If you could help, with explanations, I would appreciate! :D
Edit: iharob's answer is better than mine. Check his answer first.
Edit #2: I'm going to try to explain why your code is wrong
Consider the 2nd line of main() in your question:
int *array[n];
Let's try to read it backwards.
[n]
says we have an array that contains n elements. We don't know what type those elements are and what the name of the array is, but we know we have an array of size n.
array[n]
says your array is called array.
* array[n]
says you have a pointer to an array. The array that is being pointed to is called 'array' and has a size of n.
int * array[n];
says you have a pointer to an integer array called 'array' of size n.
At this point, you're 3/4 way to making a 2d array, since 2d arrays consist of a list of pointers to arrays. You don't want that.
Instead, what you need is:
int * array;
At this point, we need to examine your function, initArray:
int *initArray(int n){
int i;
int *array[n];
for(i = 0; i < n; i++){
array[i] = i*2;
}
return array;
}
The second line of initArray has the same mistake as the second line of main. Make it
int * array;
Now, here comes the part that's harder to explain.
int * array;
doesn't allocate space for an array. At this point, it's a humble pointer. So, how do we allocate space for an array? We use malloc()
int * array = malloc(sizeof(int));
allocates space for only one integer value. At this point, it's more a variable than an array:
[0]
int * array = malloc(sizeof(int) * n);
allocates space for n integer variables, making it an array:
e.g. n = 5:
[0][0][0][0][0]
Note:The values in the real array are probably garbage values, because malloc doesn't zero out the memory, unlike calloc. The 0s are there for simplicity.
However, malloc doesnt always work, which is why you need to check it's return value:
(malloc will make array = NULL if it isn't successful)
if (array == NULL)
return NULL;
You then need to check the value of initArray.
#include <stdio.h>
#include <stdlib.h>
int *initArray(int n){
int i;
int *array = malloc(sizeof(int) * n);
if (array == NULL)
return NULL;
for(i = 0; i < n; i++){
array[i] = i*2;
}
return array;
}
int main(){
int i, n = 5;
int *array = initArray(n);
if (array == NULL)
return 1;
printf("Here is the array: ");
for(i = 0; i < n; i++){
printf("%d ", array[i]);
}
free(array);
printf("\n\n");
return 0;
}
You can't just return an array like that. You need to make a dynamically allocated array in order to do that. Also, why did you use a 2d array anyway?
int array[5];
is basically (not completely) the same as:
int * array = malloc(sizeof(int) * 5);
The latter is a bit more flexible in that you can resize the memory that was allocated with malloc and you can return pointers from functions, like what the code I posted does.
Beware, though, because dynamic memory allocation is something you don't wanna get into if you're not ready for tons of pain and debugging :)
Also, free() anything that has been malloc'd after you're done using it and you should always check the return value for malloc() before using a pointer that has been allocated with it.
Thanks to iharob for reminding me to include this in the answer
Do you want to initialize the array? You can try it like this.
#include <stdio.h>
void initArray(int *p,int n)
{
int i;
for(i = 0; i < n; i++)
{
*(p+i) = i*2;
}
}
void main(void)
{
int i, n = 5;
int array[n];
initArray(array,n);
printf("Here is the array: ");
for(i = 0; i < n; i++)
{
printf("%d ", array[i]);
}
printf("\n\n");
}
If you don't want to get in trouble learning malloc and dynamic memory allocation you can try this
#include <stdio.h>
void initArray(int n, int array[n]) {
int i;
for (i = 0 ; i < n ; i++) {
array[i] = i * 2;
}
}
int main() { /* main should return int */
int i, n = 5;
int array[n];
initArray(n, array);
printf("Here is the array: ");
for(i = 0 ; i < n ; i++) {
printf("%d ", array[i]);
}
printf("\n\n");
return 0;
}
as you see, you don't need to return the array, if you declare it in main(), and pass it to the function you can just modify the values directly in the function.
If you want to use pointers, then
#include <stdio.h>
int *initArray(int n) {
int i;
int *array;
array = malloc(n * sizeof(*array));
if (array == NULL) /* you should always check malloc success */
return NULL;
for (i = 0 ; i < n ; i++) {
array[i] = i * 2;
}
return array;
}
int main() { /* main should return int */
int i, n = 5;
int *array;
array = initArray(n);
if (array == NULL) /* if null is returned, you can't dereference the pointer */
return -1;
printf("Here is the array: ");
for(i = 0 ; i < n ; i++) {
printf("%d ", array[i]);
}
free(array); /* you sould free the malloced pointer or you will have a memory leak */
printf("\n\n");
return 0;
}

Data Sorting Program using Pointers, Arrays, and Bubble Sorting having Syntax Errors

How would I get rid of these syntax errors that are not allowing me to compile?
This has the correct structure for what is doing but these errors are preventing me from testing it out.
I am so close to testing this. I did int array and int pointer to fix some of these errors but doing them in the lines with the next errors does not fix them.
It's all the same type of error.
Pointers.c: In function ‘ArrayInitialize’:
Pointers.c:19: error: expected ‘;’ before ‘)’ token
Pointers.c:23: error: expected ‘;’ before ‘)’ token
Pointers.c:25: warning: assignment makes integer from pointer without a cast
Pointers.c: At top level:
Pointers.c:32: error: expected ‘)’ before ‘array’
Pointers.c:44: error: expected ‘)’ before ‘array’
Pointers.c:56: error: expected ‘)’ before ‘array’
Pointers.c:78: error: expected ‘)’ before ‘pointer’
#include <stdio.h>
#include <stdlib.h>
#define SIZE_OF_ARRAY 5
//=============================================================================
int *IntegerPtr;
int ArrayInt[SIZE_OF_ARRAY];
int *ArrayPtr[SIZE_OF_ARRAY];
//-----------------------------------------------------------------------------
void ArrayInitialize(int *array,int *pointer){
int i;
srand(getpid());
for (i =0, i < SIZE_OF_ARRAY; i++;){
array[i] = (int)rand();
for (i =0, i < SIZE_OF_ARRAY; i++;){
pointer[i] = &array[i];
}
}
}
//-----------------------------------------------------------------------------
void ArrayPrint(ArrayInt array){
int i;
for (i =0, int < SIZE_OF_ARRAY; i++;){
printf("%d : %10d \n",i,array[i]);
}
printf("\n");
}
//-----------------------------------------------------------------------------
void ArrayPointerPrint(ArrayInt array){
int i;
for (i =0, i < SIZE_OF_ARRAY; i++){
printf("%d : %10d \n",i,pointer[i]);
}
printf("\n");
}
//-----------------------------------------------------------------------------
void ArrayBubbleSort(ArrayInt array){
int i;
int j;
int temp;
for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
{
for( j = 1; j <= i; j++ )
{
if( *(array+(j-1)) > *(array+j))
{
temp = *array+(j-1));
*array+(j-1)) = array+(j));
*array+(j) = temp;
}
}
}
}
//-----------------------------------------------------------------------------
void PointerBubbleSort(ArrayPtr pointer){
int i;
int j;
int temp;
for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
{
for( j = 1; j <= i; j++ )
{
if( *(pointer+(j-1)) > *(pointer+j))
{
temp = *pointer+(j-1));
*pointer+(j-1)) = pointer+(j));
*pointer+(j) = temp;
}
}
}
}
//-----------------------------------------------------------------------------
int main(void) {
int array[SIZE_OF_ARRAY];
int pointer[SIZE_OF_ARRAY];
ArrayInitialize(array,pointer);
ArrayPrint(array);
PointerBubbleSort(pointer);
ArrayPointerPrint(pointer);
ArrayBubbleSort(array);
ArrayPrint(array);
ArrayPointerPrint(pointer);
return(EXIT_SUCCESS);
}
There are a whole lot of syntax errors in this program, and your code formatting doesn't exactly make it easy to read (for example, the indentation is very inconsistent).
The syntax error gives you the line number it failed on. You should look at that line, and maybe the lines immediately around that line, to determine what went wrong.
For example:
Pointers.c:32: error: expected ‘)’ before ‘array’
line 32:
void ArrayPrint(ArrayInt array){
The error gives you a clue, that something went wrong before array. The problem here is that ArrayInt is used as a type. You haven't defined this type anywhere.
Next, the syntax of your for loop is incorrect. The correct syntax is for (initial condition; condition; increment). Look at that carefully, and compare it to the syntax you used - notice the use of ;s rather than ,s, as well as their placement (See this question if you're interested.
I recommend you fix those, and then look hard at each error, and consult a book, web resource and/or Google to see how you're supposed to implement what you wrote down there (e.g. search for "c for loop syntax".. or "c array tutorial").
Now, if you're struggeling with these, you may want to try using the LLVM clang compiler, though it might be difficult to install if you can't find a linux package for it and/or you're not on a recent Mac OS X version. It outputs some nicer errors:
% clang -o foo foo.c
foo.c:17:9: warning: implicit declaration of function 'getpid' is invalid in C99
[-Wimplicit-function-declaration]
srand(getpid());
^
foo.c:25:18: warning: incompatible pointer to integer conversion assigning to
'int' from 'int *'; remove & [-Wint-conversion]
pointer[i] = &array[i];
^ ~~~~~~~~~
foo.c:23:18: warning: expression result unused [-Wunused-value]
for (i =0, i < SIZE_OF_ARRAY; i++;){
~ ^ ~~~~~~~~~~~~~
foo.c:19:16: warning: expression result unused [-Wunused-value]
for (i =0, i < SIZE_OF_ARRAY; i++;){
~ ^ ~~~~~~~~~~~~~
foo.c:32:17: error: unknown type name 'ArrayInt'
void ArrayPrint(ArrayInt array){
^
foo.c:35:12: error: expected ';' in 'for' statement specifier
for (i =0, int < SIZE_OF_ARRAY; i++;){
^
foo.c:35:12: error: expected expression
foo.c:35:38: error: expected ')'
for (i =0, int < SIZE_OF_ARRAY; i++;){
^
foo.c:35:7: note: to match this '('
for (i =0, int < SIZE_OF_ARRAY; i++;){
^
foo.c:35:39: error: expected expression
for (i =0, int < SIZE_OF_ARRAY; i++;){
^
foo.c:44:24: error: unknown type name 'ArrayInt'
void ArrayPointerPrint(ArrayInt array){
^
foo.c:47:36: error: expected ';' in 'for' statement specifier
for (i =0, i < SIZE_OF_ARRAY; i++){
^
foo.c:48:29: error: use of undeclared identifier 'pointer'
printf("%d : %10d \n",i,pointer[i]);
^
foo.c:47:16: warning: expression result unused [-Wunused-value]
for (i =0, i < SIZE_OF_ARRAY; i++){
~ ^ ~~~~~~~~~~~~~
foo.c:56:22: error: unknown type name 'ArrayInt'
void ArrayBubbleSort(ArrayInt array){
^
foo.c:78:24: error: unknown type name 'ArrayPtr'
void PointerBubbleSort(ArrayPtr pointer){
^
5 warnings and 10 errors generated.
Try to resolve those, and then continue reading. I'll mark the rest as a spoiler so that you can give it a go first.
.
.
.
Here:
for (i =0, int < SIZE_OF_ARRAY; i++;){
you just randomly put int where i should probably go, once you've fixed your for loop syntax.
void ArrayPointerPrint(int* array){
int i;
for (i =0; i < SIZE_OF_ARRAY; i++){
printf("%d : %10d \n",i,pointer[i]);
Here: what's pointer supposed to be, asks the compiler? It exists neither in the function's scope, nor in the global scope. I guess you meant array.
temp = *array+(j-1);
Why aren't you just using array[j-1] here? While it might work in this particular line, when you try to use the same on the left hand side of the assignment it certainly doesn't. You've also got some extra brackets in this area.
The code below compiles for me. There are still some warnings left though. You should google them.
#include <stdio.h>
#include <stdlib.h>
#define SIZE_OF_ARRAY 5
//=============================================================================
int *IntegerPtr;
int ArrayInt[SIZE_OF_ARRAY];
int *ArrayPtr[SIZE_OF_ARRAY];
//-----------------------------------------------------------------------------
void ArrayInitialize(int *array,int *pointer){
int i;
srand(getpid());
for (i =0; i < SIZE_OF_ARRAY; i++){
array[i] = (int)rand();
for (i =0; i < SIZE_OF_ARRAY; i++){
pointer[i] = &array[i];
}
}
}
//-----------------------------------------------------------------------------
void ArrayPrint(int* array){
int i;
for (i =0; i < SIZE_OF_ARRAY; i++){
printf("%d : %10d \n",i,array[i]);
}
printf("\n");
}
//-----------------------------------------------------------------------------
void ArrayPointerPrint(int* array){
int i;
for (i =0; i < SIZE_OF_ARRAY; i++){
printf("%d : %10d \n",i,array[i]);
}
printf("\n");
}
//-----------------------------------------------------------------------------
void ArrayBubbleSort(int* array){
int i;
int j;
int temp;
for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
{
for( j = 1; j <= i; j++ )
{
if( *(array+(j-1)) > *(array+j))
{
temp = *array+(j-1);
array[j-1] = array[j];
array[j] = temp;
}
}
}
}
//-----------------------------------------------------------------------------
void PointerBubbleSort(int* pointer){
int i;
int j;
int temp;
for( i = (SIZE_OF_ARRAY - 1); i >= 0; i-- )
{
for( j = 1; j <= i; j++ )
{
if( *(pointer+(j-1)) > *(pointer+j))
{
temp = *pointer+(j-1);
pointer[j-1] = pointer+(j);
pointer[j] = temp;
}
}
}
}
//-----------------------------------------------------------------------------
int main(void) {
int array[SIZE_OF_ARRAY];
int pointer[SIZE_OF_ARRAY];
ArrayInitialize(array,pointer);
ArrayPrint(array);
PointerBubbleSort(pointer);
ArrayPointerPrint(pointer);
ArrayBubbleSort(array);
ArrayPrint(array);
ArrayPointerPrint(pointer);
return(EXIT_SUCCESS);
}
Two of your problems are:
for (i =0, i < SIZE_OF_ARRAY; i++;){
array[i] = (int)rand();
for (i =0, i < SIZE_OF_ARRAY; i++;){
In both the loops, the comma should be a semicolon and the final semicolon should be missing. As it is, you have an assignment (i = 0 followed by an unused comparison i < SIZE_OF_ARRAY) and the loop condition is i++ != 0 (which takes a long time to become false; you've probably crashed long before it does become false), and there is no reinitialize step. You also don't really want two nested loops both indexing on i — use i and j, instead. Thus, if you need nested loops, you write:
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = (int)rand();
for (i = 0; j < SIZE_OF_ARRAY; j++)
{
However, it looks to me like there's no need for nested loops at all:
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = rand();
pointer[i] = &array[i]; // More on this in a moment
}
The compiler is also diagnosing a problem with the assignment:
pointer[i] = &array[i];
You're assigning a pointer (&array[i]) to an int (because int *pointer means that pointer is a pointer to an int so pointer[i], equivalent to *(pointer + i), is an int). Judging from the code, it looks like in main() you should be declaring:
int *pointer[SIZE_OF_ARRAY];
and the function interface should be:
void ArrayInitialize(int *array, int **pointer)
but all sorts of other parts of the code are inconsistent with that. I'm sure it can be made consistent; the question is which way to make it consistent. There isn't a reason to use pointer at all (that I can see) unless it is int **pointer in the function.
You have another variant of the loop syntax problems at:
for (i =0, int < SIZE_OF_ARRAY; i++;){
That too should be:
for (i = 0; i < SIZE_OF_ARRAY; i++){
In the other functions, you are using the names ArrayInt and ArrayPtr as if they are typedef names, but they're actually (unused) global variables. If you make them into typedefs after all, use them consistently. If you aren't going to make them into typedefs, then get rid of them and fix the function definitions.
You have unmatched parentheses in the sort functions where you swap:
temp = *array+(j-1));
*array+(j-1)) = array+(j));
*array+(j) = temp;
The notation you're using is weird; why not just use array indexes?
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
Working code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE_OF_ARRAY 5
typedef int *IntegerPtr;
typedef int ArrayInt[SIZE_OF_ARRAY];
typedef int *ArrayPtr[SIZE_OF_ARRAY];
void PointerBubbleSort(ArrayPtr pointer);
void ArrayBubbleSort(ArrayInt array);
void ArrayPointerPrint(ArrayPtr pointer);
void ArrayPrint(ArrayInt array);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer)
{
int i;
srand(getpid());
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = (int)rand();
pointer[i] = &array[i];
}
}
void ArrayPrint(ArrayInt array)
{
int i;
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
printf("%d : %10d \n", i, array[i]);
}
printf("\n");
}
void ArrayPointerPrint(ArrayPtr pointer)
{
int i;
for (i = 0; i < SIZE_OF_ARRAY; i++)
{
printf("%d : %10d \n", i, *pointer[i]);
}
printf("\n");
}
void ArrayBubbleSort(ArrayInt array)
{
int i;
int j;
int temp;
for (i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (array[j-1] > array[j])
{
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
}
}
}
}
void PointerBubbleSort(ArrayPtr pointer)
{
int i;
int j;
int *temp;
for (i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (*pointer[j-1] > *pointer[j])
{
temp = pointer[j-1];
pointer[j-1] = pointer[j];
pointer[j] = temp;
}
}
}
}
int main(void)
{
ArrayInt array;
ArrayPtr pointer;
ArrayInitialize(array, pointer);
ArrayPrint(array);
PointerBubbleSort(pointer);
ArrayPointerPrint(pointer);
ArrayBubbleSort(array);
ArrayPrint(array);
return(EXIT_SUCCESS);
}
Sample output
0 : 881325466
1 : 1242393703
2 : 927466540
3 : 1493827854
4 : 533425101
0 : 533425101
1 : 881325466
2 : 927466540
3 : 1242393703
4 : 1493827854
0 : 533425101
1 : 881325466
2 : 927466540
3 : 1242393703
4 : 1493827854
Revised code
Minor tweaks to the code to adhere to the letter of the assignment. In particular, it uses the otherwise unused type definition for 'pointer to integer' to build the 'array of five pointers to integer'. It tries to get the output format to match the question's output precisely. Try to avoid trailing spaces on output lines (and source code lines).
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
enum { SIZE_OF_ARRAY = 5 };
typedef int *IntPtr;
typedef int ArrayInt[SIZE_OF_ARRAY];
typedef IntPtr ArrayPtr[SIZE_OF_ARRAY];
void PointerBubbleSort(ArrayPtr pointer);
void ArrayBubbleSort(ArrayInt array);
void ArrayPointerPrint(ArrayPtr pointer);
void ArrayPrint(ArrayInt array);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer);
void ArrayInitialize(ArrayInt array, ArrayPtr pointer)
{
srand(getpid());
for (int i = 0; i < SIZE_OF_ARRAY; i++)
{
array[i] = rand();
pointer[i] = &array[i];
}
}
void ArrayPrint(ArrayInt array)
{
for (int i = 0; i < SIZE_OF_ARRAY; i++)
printf("%2d : %10d\n", i, array[i]);
}
void ArrayPointerPrint(ArrayPtr pointer)
{
for (int i = 0; i < SIZE_OF_ARRAY; i++)
printf("%2d : %10d\n", i, *pointer[i]);
}
void ArrayBubbleSort(ArrayInt array)
{
for (int i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (*(array + j-1) > *(array + j))
{
int temp = *(array + j-1);
*(array + j-1) = *(array + j);
*(array + j) = temp;
}
}
}
}
void PointerBubbleSort(ArrayPtr pointer)
{
for (int i = (SIZE_OF_ARRAY - 1); i >= 0; i--)
{
for (int j = 1; j <= i; j++)
{
if (*pointer[j-1] > *pointer[j])
{
int *temp = pointer[j-1];
pointer[j-1] = pointer[j];
pointer[j] = temp;
}
}
}
}
int main(void)
{
ArrayInt array;
ArrayPtr pointer;
ArrayInitialize(array, pointer);
puts("---- Initialized array of integers ----");
ArrayPrint(array);
PointerBubbleSort(pointer);
puts("---- Sorted array of pointers ----");
ArrayPointerPrint(pointer);
ArrayBubbleSort(array);
puts("---- Sorted array of integers ----");
ArrayPrint(array);
puts("---- Array of pointers ----");
ArrayPointerPrint(pointer);
return(EXIT_SUCCESS);
}
Revised output
---- Initialized array of integers ----
0 : 974520281
1 : 2052070745
2 : 565640395
3 : 1955497143
4 : 950748713
---- Sorted array of pointers ----
0 : 565640395
1 : 950748713
2 : 974520281
3 : 1955497143
4 : 2052070745
---- Sorted array of integers ----
0 : 565640395
1 : 950748713
2 : 974520281
3 : 1955497143
4 : 2052070745
---- Array of pointers ----
0 : 974520281
1 : 2052070745
2 : 565640395
3 : 1955497143
4 : 950748713

Resources