I am trying to do my homework but I got stuck. They want me to take one array which is already given and separate it into two arrays, one of them holds the even numbers and the other holds the odd numbers. I wrote a void function that receives 6 parameters as I will show below. The if statement: (if ((arr[j]%2) == 0)) in the function does not get executed for some reason. It just skips it. I don't really understand why and I'd appreciate any assistance.
Tried debugging, using different syntax for the pointers Arr1 and Arr2.
#include <stdio.h>
#include <malloc.h>
void separate(int* arr, int n, int* size1, int* size2, int* arr1, int* arr2);
int main()
{
int size1=0, size2=0;
int* newArr1 = NULL;
int* newArr2 = NULL;
int arr[] = { 6,57,14,21,11,3,22,42,9,15 };
printf("The array before change:\n");
for (int i = 0; i <10; i++)
{
printf(" %d", arr[i]);
}
printf("\n");
separate(arr, 10, &size1, &size2, newArr1, newArr2);
printf("The even array is:\n");
for (int i = 0; i <size1; i++)
{
printf(" %d", newArr1[i]);
}
printf("\n");
printf("The odd array is:\n");
for (int i = 0; i <size2; i++)
{
printf(" %d", newArr2[i]);
}
printf("\n");
system("pause");
return 0;
}
void separate(int* arr, int n, int* size1, int* size2, int* arr1, int* arr2)
{
int i, j;
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
(*size1)++;
else
(*size2)++;
}
printf("\n");
printf("size1: %d size2: %d", (*size1),(*size2));
arr1 = (int*)calloc((*size1), sizeof(int));
arr2 = (int*)calloc((*size2), sizeof(int));
for (j = 0; j < n; j++)
{
if ((arr[j]%2) == 0)
arr1[j] == arr[j];
}
for (j = 0; j < n; j++)
{
if (arr[j] % 2 != 0)
arr2[j]== arr[j];
}
return;
}
Does not compile
Turn on warnings! You're trying to use a '==' for assignment - should be '='.
gcc -std=c99 -Wall omg.c -o omg
omg.c: In function 'main':
omg.c:32:5: warning: implicit declaration of function 'system' [-Wimplicit-function-declaration]
system("pause");
^
omg.c: In function 'separate':
omg.c:55:9: warning: statement with no effect [-Wunused-value]
arr1[j] == arr[j];
^
omg.c:61:13: warning: statement with no effect [-Wunused-value]
arr2[j]== arr[j];
^
This is wrong
for (j = 0; j < n; j++)
{
if ((arr[j]%2) == 0)
arr1[j] == arr[j];
}
Imagine j being the last one (n - 1). You will try to set arr1[n - 1] to whatever, but size of arr1 is size1 not n.
As others pointed out you are using == to try to assign values.
Your array is going out of bounds because you allocated only enough memory in your other arrays to hold the amount of even/odd numbers in the array that is being sorted. I left comments for you. Idk what compiler or ide your using but I got this working on Visual Studio, with some other changes to the code. I am also a student!
void separate(int* arr, int n, int* size1, int* size2, int* arr1, int* arr2)
{
int i, j;
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
(*size1)++;
else
(*size2)++;
}
printf("\n");
printf("size1: %d size2: %d", (*size1), (*size2));
// Your assigning only enough space to hold the amount of even/odd numbers
arr1 = (int*)calloc((*size1), sizeof(int));
arr2 = (int*)calloc((*size2), sizeof(int));
// If the index of the array is larger than what you allocated, crash..
for (j = 0; j < n; j++)
{
if ((arr[j] % 2) == 0)
arr1[j] == arr[j];
}
for (j = 0; j < n; j++)
{
if (arr[j] % 2 != 0)
arr2[j] == arr[j]; // Use = to assign, not ==
}
return;
}
Related
So im working on this program that is supposed to take the pointer to an array and the array’s size (number of elements in the array)
as arguments, finds the place the index of the outlier, fixes the array in place (that is puts the outlier to a
place it is supposed to be), and returns the old index where the outlier was found. i finished my code but for some reason, somewhere in my main function its telling me there is a segmentation fault, i know its in my main function because it compiled and ran fine when it was just the original code. heres the code;
#include <stdio.h>
long long int fix_sorted_array(double* arr, unsigned long n)
{
double temp;
int i, j;
for ( i = 0; i < n - 1; i ++)
{
if (arr[i] > arr[i + 1])
{
for ( j = i + 1; j > 0; j --)
{
if (arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
return i + 1;
}
}
return -1;
}
int main()
{
int n;
int j;//declared variables
double arr[n];
printf("Enter elements of array : \n");
for ( int i = 0; i < n; i ++)
{
scanf("%lf", &arr[i]);
}
printf("Return index : %lld\n",fix_sorted_array (&arr[n], n));
printf("Array after : \n");
for ( j = 0; j < n; j ++)
{
printf("%.2lf", arr[j]);
}
}
You're passing an address outside the array to the function in this line:
printf("Return index : %lld\n",fix_sorted_array (&arr[n], n));
You want to pass the address of the start of the array, not the end, so it should be:
printf("Return index : %lld\n",fix_sorted_array (arr, n));
You also need to initialize n before you declare the array.
printf("How many numbers? ");
scanf("%d", &n);
double arr[n];
You have never accepted the value of n. Below code might help.
#include <stdio.h>
long long int fix_sorted_array(double* arr, unsigned long n)
{
double temp;
int i, j;
for ( i = 0; i < n - 1; i ++)
{
if (arr[i] > arr[i + 1])
{
for ( j = i + 1; j > 0; j --)
{
if (arr[j] < arr[j-1])
{
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
}
}
return i + 1;
}
}
return -1;
}
int main()
{
int n;
int j;//declared variables
printf("Enter the number of elements in arrays");
scanf("%d",&n); // initialize the values of n
double arr[n];
printf("Enter elements of array : \n");
for ( int i = 0; i < n; i ++)
{
scanf("%lf", &arr[i]);
}
printf("Return index : %lld\n",fix_sorted_array (&arr[0], n)); // Also pass the value of starting index in array i.e. `arr[0]`
printf("Array after : \n");
for ( j = 0; j < n; j ++)
{
printf("%.2lf", arr[j]);
}
}
I have this program that I try to run:
void get_set(int size, int arr[])
{
int i;
printf("Enter number of values to the array : \n");
scanf("%d", &size);
printf("\n");
for (i = 0; i < size; i++)
{
printf("Value at %dth place is : \n", i + 1);
if (scanf("%d", &arr[i]) == EOF)
{
break;
}
}
}
void print_set(int size, int arr[])
{
int i, flag = 0;
if (flag == 0)
{
printf("\nOriginal array is : ");
for (i = 0; i < size; i++)
{
printf("%d,", arr[i]);
}
printf("\n");
}
else
{
printf("\nArray after deleting duplicates : ");
for (i = 0; i < size; i++)
{
printf("%d,", arr[i]);
}
printf("\n");
}
flag + 1;
}
void RemoveDuplicates(int size, int arr[])
{
int i, j, k;
for (i = 0; i < size; i++)
{
for (j = 0; j < i; j++)
{
if (arr[i] == arr[j])
{
size--;
for (k = i; k < size; k++)
{
arr[k] = arr[k + 1];
}
i--;
}
}
}
}
int main()
{
int size = 0;
int arr[64] = {0};
get_set(size, arr);
print_set(size, arr);
RemoveDuplicates(size, arr);
print_set(size, arr);
return 0;
}
In short, the program gets an array with values that I entered, and passes it thru other functions.
I can't get it to pass the array and size to other functions. I am trying to avoid globals; what am I doing wrong?
The size variable in main is passed by value to get_set, so it will still be 0 in main. You should have get_set return the size and assign the result to the size variable (or pass size by reference) so that you can pass it to print_set and RemoveDuplicates.
get_set doesn't really need size as a parameter, unless you change it's meaning to indicate the capacity of the array and add error checking to make sure that you don't overflow it.
The arr variable is passed by reference because it is an array which decays into a pointer, so get_set will modify the variable in main.
You could use:
#include <stdio.h>
#include <stdlib.h>
static int get_set(int size, int arr[])
{
int i;
int n;
printf("Enter number of values to the array: ");
fflush(stdout); // Usually not strictly necessary, but ensures the prompt appears
if (scanf("%d", &n) != 1)
{
fprintf(stderr, "Invalid number entered\n");
exit(EXIT_FAILURE);
}
if (n > size)
n = size;
printf("\n");
for (i = 0; i < n; i++)
{
printf("Value %d is: ", i + 1);
fflush(stdout);
if (scanf("%d", &arr[i]) != 1)
{
break;
}
}
return i;
}
static void print_set(const char *tag, int size, int arr[])
{
int i;
printf("\n%s: ", tag);
for (i = 0; i < size; i++)
{
printf("%d,", arr[i]);
}
printf("\n");
}
static int RemoveDuplicates(int size, int arr[])
{
int i, j, k;
for (i = 0; i < size; i++)
{
for (j = 0; j < i; j++)
{
if (arr[i] == arr[j])
{
size--;
for (k = i; k < size; k++)
{
arr[k] = arr[k + 1];
}
i--;
}
}
}
return size;
}
int main(void)
{
int arr[64] = {0};
int size = get_set(64, arr); // 64 is the maximum; size contains the actual
print_set("Original array", size, arr);
size = RemoveDuplicates(size, arr);
print_set("Duplicates removed", size, arr);
return 0;
}
None of the functions except main() are called from outside this file; the functions can all be static, therefore.
Example run:
Enter number of values to the array: 12
Value 1 is: 1
Value 2 is: 1
Value 3 is: 1
Value 4 is: 2
Value 5 is: 2
Value 6 is: 3
Value 7 is: 4
Value 8 is: 5
Value 9 is: 99
Value 10 is: 999
Value 11 is: 1
Value 12 is: 1
Original array: 1,1,1,2,2,3,4,5,99,999,1,1,
Duplicates removed: 1,2,3,4,5,99,999,
An alternative redesign of the print_set() function would make the flag in the original code into a static int. However, that is a far less flexible solution than passing the tag string argument to the function. Static variables inside functions are occasionally useful, but they should be regarded with a jaundiced eye and avoided when possible, just as global variables should be avoided when possible.
I have been trying to write a function to remove duplicate elements in an array of ints without sorting it.
For that task, I created a function named removeDuplicateElements, which gets an array and its string, and returns a new dynamically allocated array, which is a copy of the original array with removal of all duplicates elements. This function also returns by reference the size of the new array.
I also used in my code functions which build a dynamic array and print it.
Here is my code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void printArray(int *arr, int size);
int *buildArray(int size);
int *removeDuplicateElements(int *arr, int size, int *newSize);
void main() {
int size,newSize;
int *arr;
int *newArr;
printf("please enter a number for the size of array: ");
scanf("%d", &size);
printf("\nenter %d numbers: ", size);
arr = buildArray(size);
printf("\nthe array after removing the duplicate elements is: ");
newArr = removeDuplicateElements(arr, size, &newSize);
printArray(newArr, newSize);
free(newArr);
free(arr);
}
/* this function removes all duplicate elements in a given array */
int *removeDuplicateElements(int *arr, int size, int *newSize) {
int *newArr;
int count = size, i, j;
/* finding the new size of the original array with removal its duplicate elements */
for (i = 1; i < size; i++) {
for (j = 0; j < size; j++)
if (arr[i] == arr[j] && i != j) {
count--;
break;
}
}
newArr = (int*)malloc(count * sizeof(int)); /* dynamically allocating the new array */
count = 1;
newArr[0] = arr[0];
/*adding the elements in the new array without changing the order*/
for (i = 1; i < size; i++) {
for (j = 0; j < size; j++) {
if (arr[i] == arr[j] && i != j) {
break;
}
if (j == size - 1) {
newArr[count] = arr[i];
count++;
}
}
}
*newSize = count; /* updating the size of the new array */
return newArr; /* returning the address of new array */
}
void printArray(int *arr, int size) {
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int *buildArray(int size) {
int i;
int *arr = (int*)malloc(size * sizeof(int));
if (!arr) {
printf("ERROR! Not enough memory!\n");
exit(1);
}
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
return arr;
}
I get a wrong output for that code, and I dont understand why
For instance, for the following array with size=5 :1 1 3 1 3
I get the wrong output 1, whereas the expected outout is
1 3.
Any help would be appreciated.
You're firstly calculating the size of the new array incorrectly. For your example input, when you're looking at the first 3, it scans the whole array to see how many 3's there are and finds there are 2 and concludes it's a duplicate. It then does the exact same thing for the 2nd 3. So you end up with the size of the new array as 1.
Instead of scanning the whole array, you only want to scan the array for the elements preceding the one you're checking. So something like this.
for(i=1;i<size;i++)
{
for (j = 0; j < i; j++)
if (arr[i] == arr[j])
{
count--;
break;
}
}
And for the code that fills the new array has the same problem
for(i=1;i<size;i++)
{
for (j = 0; j < i; j++)
if (arr[i] == arr[j])
{
break;
}
if(j==i)
{
newArr[count++]=arr[i];
}
}
There is an alternative way of doing it, ofcourse it involves modifying the original array but this is just an alternative. Basically it involves crossing off the duplicate element by replacing it with a maximum value like 0xFFFF.
int* removeDuplicateElements(int *arr, int size, int *newSize)
{
int *newArr;
int count = size, i, j;
int index = 0;
/*finding the new size of the original array with removal its duplicate elements*/
for(i=0;i<size;i++)
{
for (j = i+1; j < size; j++)
if (arr[i] == arr[j] && arr[i] != 0xFFFF)
{
count--;
arr[j] = 0xFFFF;
}
}
printf("Size is %d \n", count);
newArr = (int*)malloc(count * sizeof(int)); /*dynamically allocating the new array*/
for(i=0;i<size;i++)
{
if(arr[i] != 0xFFFF)
newArr[index++] = arr[i];
}
*newSize = count; /*updating the size of the new array*/
return newArr; /*returning the address of new array*/
}
Hi wanted to display repeated elements of a Random array whose size can be specified by the user. The problem I am getting in the output is , the function is printing a repeated number as many times as it has been repeated but I want to print it only once.
Here is my code and output following the former:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int array_size = 0;
int *my_array;
int i = 0;
printf("Enter the size of the array:\n");
scanf("%d",&array_size);
my_array = malloc(array_size * sizeof my_array[0]);
if(NULL == my_array) {
fprintf(stderr,"MEMORY ALLOLCATION FAILED \n");
return EXIT_FAILURE;
}
for(i=0;i<array_size;i++){
my_array[i] = rand()%array_size;
}
printf("What's in the array:\n");
for(i = 0;i<array_size;i++){
printf("%d ",my_array[i]);
}
printf("\n");
display_repeats(my_array, array_size);
free(my_array);
return EXIT_SUCCESS;
}
void display_repeats(int *a,int n){
int *repeats;
repeats = malloc(n * sizeof repeats[0]);
int i=0;
int j=0;
int count = 0;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i] == a[j]){
count++;
}
}
if(count>1){
repeats[i] = count;
printf("%d occurs %d times\n",a[i],repeats[i]);
}
count = 0;
}
free(repeats);
}
Here is the output I am getting
Enter the size of the array:
5
What's in the array:
3 1 2 0 3
3 occurs 2 times
3 occurs 2 times
I want "3 occurs 2 times" to print once.
Please help!
What's happening is that your code realizes 3 is repeated twice, in this block:
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i] == a[j]){
count++;
}
}
if(count>1){
repeats[i] = count;
printf("%d occurs %d times\n",a[i],repeats[i]);
}
}
When i is equal to 0, it will look at the array and realize that 3 is repeated, so count>1 will be true. Then, when i is equal to 4, count>1 will be true again, and you get the double print.
In order to fix this, I would create an array that stores the numbers that have already been verified as repeated and check against that.
Since in your case, value of elements < size_array, so can take this approach.
memset(repeats, 0, n*sizeof(repeats[0]));
for(i=0;i<n;i++){
for(j=0;j<n;j++){
count = repeats[a[i]];
if(count>0)
break;
if(a[i] == a[j]){
count++;
}
}
repeats[a[i]] = count;
The idea is to store the count for each value and check before starting search for each new value to check if it has already been counted.
Try it:
for(i=0;i<n-1;i++)
{
for(j=1;j<n;j++)
{
So, don't compare the same one element with itself.
Thanks guys I got it working with the above ideas!
here is what I got..
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int array_size = 0;
int *my_array;
int i = 0;
printf("Enter the size of the array:\n");
scanf("%d",&array_size);
my_array = malloc(array_size * sizeof my_array[0]);
if(NULL == my_array) {
fprintf(stderr,"MEMORY ALLOLCATION FAILED \n");
return EXIT_FAILURE;
}
for(i=0;i<array_size;i++){
my_array[i] = rand()%array_size;
}
printf("What's in the array:\n");
for(i = 0;i<array_size;i++){
printf("%d ",my_array[i]);
}
printf("\n");
display_repeats(my_array, array_size);
free(my_array);
return EXIT_SUCCESS;
}
void display_repeats(int *a,int n){
int *freq;
freq = malloc(n * sizeof freq[0]);
int i=0;
int j=0;
int count = 0;
for(i=0;i<n;i++){
freq[i] = -1;
}
for(i=0;i<n;i++){
count = 1;
for(j=i+1;j<n;j++){
if(a[i]==a[j]){
count++;
freq[j] = 0;
}
}
if(freq[i]!=0){
freq[i] = count;
}
}
for(i=0;i<n;i++){
if(freq[i]!=1 && freq[i]!=0){
printf("%d occurs %d times\n",a[i],freq[i]);
}
}
free(freq);
}
In display_repeats(), you may simply update 2 lines and add 3 lines to make your program have the correct behaviour. And we will see it later: we can optimize the final code using C99 syntax and the comma C operator, to write far less lines than your original code (9 lines instead of 20 lines!).
So, in this answer, you will find how to get the final code that has the correct behaviour and is very short, shorter than your initial code:
void display_repeats(int *a, int n) {
for (int i = 0; i < n; i++) {
if (a[i] == -1) continue;
int count = 1;
for (int j = i + 1; j < n; j++)
if (a[i] == a[j]) count++, a[j] = -1;
if (count > 1) printf("%d occurs %d times\n", a[i], count);
}
}
The idea is to set to -1 each array value that has matched a previous one, just after the count increment. Because you do not want to count this value again.
So simply do the following:
In the two lines where you write count = 0, replace 0 by 1, because you are sure that each number in the list must be counted at least one time.
This way, you can avoid checking the case where i equals j in the inner loop: it is already taken into account in count. So add if (i == j) continue; at the beginning of the inner loop.
With the previous updates, you are now sure that when you increment count, in the inner loop, j is not equal to i. Therefore, you can change the value of a[j] without changing a[i], in the array.
So, add a[j] = -1; just after having increased count. This way, when i will be incremented to check for a new count of a new value, it is impossible that the new counted value has already been counted.
Finally, you do not want to count how many times -1 is in the array. But you have replaced some values with -1. So simply add if (a[i] == -1) continue; at the beginning of the outer loop to avoid counting how -1 there are in the array.
This intermediate code is:
void display_repeats(int *a,int n) {
int *repeats;
repeats = malloc(n * sizeof repeats[0]);
int i=0;
int j=0;
int count = 1;
for(i=0; i<n; i++) {
if (a[i] == -1) continue;
for(j=0; j<n; j++) {
if (i == j) continue;
if(a[i] == a[j]) {
count++;
a[j] = -1;
}
}
if(count > 1) {
repeats[i] = count;
printf("%d occurs %d times\n",a[i],repeats[i]);
}
count = 1;
}
free(repeats);
}
Now, we can optimize this code.
First, we can avoid testing a[i] == a[j] when j <= i: if such a case happens, we know that we have previously displayed the count for a[j] (the number of times a[j] appears in the array). Therefore we can replace for (j=0; j < n; j++) { by for (j=i+1; j<n; j++) {.
With this last update, we know that the first line of the inner loop will never match: i can not be equal to j. So we can remove this line (if (i == j) continue;).
Note that the values stored in the repeats array are only used to get the value of count in the printf() call. So we can remove every reference to the repeats array and simply use count in the printf() call.
Now, you can see that we set count to 1, two times. We can do it only one time, if we do not set it to 1 at the end of the main loop, to prepare a new loop, but at the beginning.
Now, note that i, j and count have the same type, so only one line may be used to define them: int i = 0, j = 0, count = 1;. More over, i and j are defined later in the for loops, so no need to define their initial value here. So we can simply write int i, j, count = 1;. But count is now defined at the beginning of the outer loop, so we do not need to define it previously. So we only need to define i, j and count without an initial value: int i, j, count;.
The new intermediate code is:
void display_repeats(int *a, int n) {
int i, j, count;
for (i = 0; i < n; i++) {
if (a[i] == -1) continue;
count = 1;
for(j = i + 1; j < n; j++) {
if (a[i] == a[j]) {
count++;
a[j] = -1;
}
}
if (count > 1) printf("%d occurs %d times\n", a[i], count);
}
}
Using C99 syntax
But we can do more, using the C99 specification: we can define a variable with the for instruction: we can write for (int i = ...). No more need to define it previously. So we can avoid having to write int i, j, count;, we will define them at first use:
void display_repeats(int *a, int n) {
for (int i = 0; i < n; i++) {
if (a[i] == -1) continue;
int count = 1;
for(int j = i + 1; j < n; j++) {
if (a[i] == a[j]) {
count++;
a[j] = -1;
}
}
if (count > 1) printf("%d occurs %d times\n", a[i], count);
}
}
Using the comma C operator
Again, we can do much better! We can use the comma operator (,): it is a binary operator that evaluates its first operand, discards the result, evaluates the second operand and returns its value. Using the comma operator, we can transform count++; a[j] = -1; to a single instruction: a[j] = (count++, -1). But we can avoid the parenthesis writing count++, a[i] = -1. Now, we do not need a block for the if statement, since there is only one instruction. Therefore, we can remove a lot of parenthesis.
The final code is:
void display_repeats(int *a, int n) {
for (int i = 0; i < n; i++) {
if (a[i] == -1) continue;
int count = 1;
for (int j = i + 1; j < n; j++)
if (a[i] == a[j]) count++, a[j] = -1;
if (count > 1) printf("%d occurs %d times\n", a[i], count);
}
}
i am having error while running this code
negativenoinmatrix.c:10:16: error: subscripted value is neither array nor pointer nor vector
if(z[i][j]<0)
i want to calculate the number of negative integers in a matrix
#include <stdio.h>
int negnumbers(int *z, int n, int m)
{
int count = 0;
int i = 0;
int j = m - 1;
while (j >= 0 && i < n)
{
if (z[i][j] < 0)
{
count += (j + 1);
i += 1;
}
else
j -= -1;
}
return count;
}
int main()
{
int n = 3, m = 4;
int a[n][m];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
scanf("%d", &a[i][j]);
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
printf("%d ", a[i][j]);
printf("\n");
}
int val = negnumbers((int *) a, 3, 4);
printf("%d", val);
}
The function needs to accept a pointer to an array, not a pointer to a single item. Change it to
int negnumbers(int n, int m, int z[n][m])
...
int val = negnumbers(3, 4, a);
(Where int z[n][m], as per the rule of "array adjustment", will get changed by the compiler internally to a pointer to the first element, int (*z)[m].)
When you pass a 2-d array to a function, at least the 2nd dimension must be specified. Change to this:
int negnumbers(int z[][4],int n,int m)
You can then use this more straightforward approach to counting the negative numbers:
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (z[i][j] < 0)
count++;
}
}
You are calling a pointer z, and also creating a dynamic matrix out of it. So you need to allocate some memory for it which can be done with:
malloc(z[i][j])
Then after you're done, make sure you deallocate the memory now or else you'll have a memory leak, which you can read more about at Memory Leaks wikipedia.
This is done by calling free(...)
Hope this solves the not an array or pointer error!