I want to create a dynamic dimensional matrix by taking the type variable from the user through the terminal. These types will be integer, float and double type matrices. But my function and matrix that I have defined in void type cannot do this job and I get an error. I think my problem is void dereferencing but I can't solve it. Thank you very much if you help.
I run it like this from terminal
./run arg1 arg2 i arg4 ...
i = integer
f = float
d = double
The function I have written is as follows
void ** random_matrice_gen(int row, int column, int upper, int lower, char * option){
void **matrice;
if(strcmp(option,"i") == 0){
matrice = malloc(row*sizeof(int *));
for(int i = 0 ; i < row ; i++){
matrice[i] = malloc( column*sizeof(int) );
}
}
else if(strcmp(option,"f") == 0){
matrice = malloc(row*sizeof(float *));
for(int i = 0 ; i < row ; i++){
matrice[i] = malloc( column*sizeof(float) );
}
} else {
matrice = malloc(row*sizeof(double *));
for(int i = 0 ; i < row ; i++){
matrice[i] = malloc( column*sizeof(double) );
}
}
srand((unsigned)time(0));
for(int i=0; i<row; i++){
for(int j=0; j<column; j++){
if(strncmp(option,"i", 1) == 0){
int rand_num_i = ((int) rand() / RAND_MAX) * (int) ((upper- lower ) + lower);
matrice[i][j] = rand_num_i;
}
else if(strncmp(option,"f" , 1) == 0){
float rand_num_f = ((float) rand() / RAND_MAX) * (float)((upper- lower ) + lower);
printf("%f debug f\n", rand_num_f);
matrice[i][j] = rand_num_f;
} else {
double rand_num_d = ((double) rand() / RAND_MAX) * (double) ((upper- lower) + lower);
matrice[i][j] = rand_num_d;
}
}
}
return matrice;}
Errors as follows
'''50:15: warning: dereferencing ‘void *’ pointer
50 | matrice[i][j] = rand_num_i;
| ^
matrixgen.c:50:19: error: invalid use of void expression
50 | matrice[i][j] = rand_num_i;
| ^
matrisgen.c:57:15: warning: dereferencing ‘void *’ pointer
57 | matrice[i][j] = rand_num_f;
| ^
matrixgen.c:57:19: error: invalid use of void expression
57 | matrice[i][j] = rand_num_f;
| ^
matrixgen.c:61:15: warning: dereferencing ‘void *’ pointer
61 | matrice[i][j] = rand_num_d;
| ^
matrixgen.c:61:19: error: invalid use of void expression
61 | matrice[i][j] = rand_num_d;
'''
(void *) is a pointer to something unknown, as such, the size isn't known so you can't perform pointer arithmetic over it.
You need to cast it beforehand, ie
((int **)matrice)[i][j] = rand_num_i;
does compile.
It's not possible to dereference void * pointers, and matrice[i] is such a pointer (and array indexing is equal to pointer dereference).
You need to cast this pointer to the correct type:
((int *) matrice[i])[j] = rand_num_i;
Related
I dont understand some small actions that has taken in this code, for example
i) why do we need to do &rs, why cant we just write
int returnSize;
p = twoSum(a, sizeof(a)/sizeof(a[0]), target, int*returnSize);
if(returnSize == 0)
instead of &rs... what is wrong with doing this?
i ran the code here:
#include <stdio.h>
#include <stdlib.h>
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int i, j;
int* ret_arr = (int*)malloc(2*sizeof(int));
if(ret_arr == NULL){
// *returnSize = 0;
returnSize = 0;
return NULL;
}
for(i = 0; i< numsSize; i++){
for(j = i+1; j< numsSize; j++){
if(nums[i] + nums[j] == target){
//*returnSize = 2;
returnSize = 2;
ret_arr[0] = I;
ret_arr[1] = j;
return ret_arr;
}
}
}
//*returnSize = 0;
returnSize = 0;
free(ret_arr);
return NULL;
}
int main()
{
int a[] = {2,7,11,15};
int returnSize, target = 18;
int *p = NULL;
p = twoSum(a, sizeof(a)/sizeof(a[0]), target, int*returnSize);
//if(*returnSize == 0){
if(returnSize == 0){
printf("target not found");
}else{
printf("target found at indices %d and %d/n", p[0], p[1]);
free(p);
}
return 0;
}
error result:
main.c: In function ‘twoSum’:
main.c:34:28: warning: assignment to ‘int *’ from ‘int’ makes
pointer from integer without a cast [-Wint-conversion]
34 | returnSize = 2;
| ^
main.c: In function ‘main’:
main.c:57:48: error: expected expression before ‘int’
57 | p = twoSum(a, sizeof(a)/sizeof(a[0]), target,
int*returnSize);
| ^~~
full code: https://onlinegdb.com/7OpTpwxZy3
i dont know how this doesn't make sense to the compiler.
ii) Pointer array conceptual Q
we need to declare pointer p in line 60 because to access returned array elements (of two sum function) and print them (like they are used in line ), on screen from main. QUESTION: WHY DO WE NEED POINTER FOR TWO SUM FUNCTION WHEN WE ALREADY HAVE pointer p DECLARED IN MAIN, ITS LIKE POINTER POINTING TO A POINTER(LIKE int*a, int*p, p = a), why does the code work?
error i get if i remove "int*" in line 4(2nd pic), in front of twosum function.
main.c:28:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
28 | twoSum(int* nums, int numsSize, int target, int* returnSize){
| ^~~~~~
main.c: In function ‘twoSum’:
main.c:34:16: warning: returning ‘void *’ from a function with return type ‘int’ makes integer from pointer without a cast [-Wint-conversion]
34 | return NULL;
| ^~~~
main.c:43:24: warning: returning ‘int *’ from a function with return type ‘int’ makes integer from pointer without a cast [-Wint-conversion]
43 | return ret_arr;
| ^~~~~~~
main.c:49:12: warning: returning ‘void *’ from a function with
return type ‘int’ makes integer from pointer without a cast [-
Wint-conversion]
49 | return NULL;
| ^~~~
main.c: In function ‘main’:
main.c:60:4: warning: assignment to ‘int *’ from ‘int’ makes
pointer from integer without a cast [-Wint-conversion]
60 | p = twoSum(a, sizeof(a)/sizeof(a[0]), target, &rs);
| ^
here is the full code for reference:
#include <stdio.h>
#include <stdlib.h>
twoSum(int* nums, int numsSize, int target, int* returnSize){
int i, j;
int* ret_arr = (int*)malloc(2*sizeof(int));
if(ret_arr == NULL){
*returnSize = 0;
return NULL;
}
for(i = 0; i< numsSize; i++){
for(j = i+1; j< numsSize; j++){
if(nums[i] + nums[j] == target){
*returnSize = 2;
ret_arr[0] = I;
ret_arr[1] = j;
return ret_arr;
}
}
}
*returnSize = 0;
free(ret_arr);
return NULL;
}
int main()
{
int a[] = {2,7,11,15};
int rs, target = 18;
int *p = NULL;
p = twoSum(a, sizeof(a)/sizeof(a[0]), target, &rs);
if(rs == 0){
printf("target not found");
}else{
printf("target found at indices %d and %d/n", p[0], p[1]);
free(p);
}
return 0;
}
i hope i provided enough information to understand the question, Question might be fairly simple for experienced programmers but i am just a beginner. Also please explain in easy simple words if you can thinking you are explaining to a beginner.
writing image as texts: i am not sure what to do because in this case image seems easier that text.
i) i tried changing the code as shown below in main function as well as in twosum function.
int returnSize;
p = twoSum(a, sizeof(a)/sizeof(a[0]), target, int*returnSize);
if(returnSize == 0)
ii) i have tried removing int* in line 4(2nd pic), in front of twosum function but i got an error shown here:
There seems to be a basic misunderstanding of what pointers are, how they works and why are used in the posted program.
The function twoSum is trying to find the two elements in an array whose sum equals a given target. The author of this snippet made the following design choices:
// The function returns a POINTER to some memory, allocated in the free store,
// where the two indeces will be stored, or NULL if no elements satisfy the constraint.
int*
twoSum( int* nums, int numsSize // The array is passed as pointer to its first
// element and size (number of elements)
, int target
, int* returnSize ) // This POINTER is used to "return" the size
// of the memory allocated in this function
// to the caller. This may be referred to as
// an "out parameter", it allows the function
// to change the value of a variable stored elsewhere.
{
int i, j;
int* ret_arr = (int*)malloc(2*sizeof(int));
if(ret_arr == NULL){
*returnSize = 0;
// ^ The pointer must be DEREFERENCED to access the value of the pointee.
return NULL;
}
/* [...] */
}
int main()
{
int a[] = {2,7,11,15};
int returnSize, target = 18;
//^^^^^^^^^^^^ This variable is declared as an int.
int *p = NULL;
p = twoSum(a, sizeof(a)/sizeof(a[0]), target, &returnSize);
// We need to pass its ADDRESS here ^^^^^^^^^^^
// because this function is expecting a POINTER to int.
if( returnSize == 0){
// ^^^^^^^^^^ Beeing an int, it must be used as an int value.
printf("target not found");
} else {
printf("target found at indices %d and %d/n", p[0], p[1]);
free(p); // <- Correct. The caller has this responsability.
}
return 0;
}
To answer the first point, it needs the & because it needs to take the address of the variable returnSize declared in main as an int. That address is passed to the function where it is used to initialize the local variable returnSize, which is declared in the signature of the function as a parameter of type int *, a pointer. Please note that those are two different objects, in different scopes, with different lifetimes and different type.
The call can't be written as
p = twoSum(a, sizeof(a)/sizeof(a[0]), target, int*returnSize);
// ^^^^ Syntax error, this isn't a declaration
why do we need pointer for twoSum function when we already have poiner p declared in main?
I'm not sure what the OP's doubts are here. The variable declared in main needs to be assigned a meaningful value, so it uses the value returned by the function. Inside twoSum, a variable of type pointer is needed to allocate the required memory and that is returned to main.
This isn't the only way to accoplish this task, in fact we might prefer to not allocate any memeory inside the function and pass an address instead:
#include <stdio.h>
#include <stdlib.h>
// If there exist two elements whose sum equals the 'target', the function returns 1
// and the indices of these elements are stored in the array 'indices'.
// Otherwise, the function returns 0 and doesn't modify 'indices'.
int
find_indices_of_sum( size_t n, int const* arr
, int target
, int *indices )
{
for(size_t i = 0; i < n; ++i)
{
for(size_t j = i + 1; j < n; ++j)
{
if( arr[i] + arr[j] == target )
{
indices[0] = i;
indices[1] = j;
return 1;
}
}
}
return 0;
}
int main(void)
{
int a[] = {2, 7, 11, 15};
size_t a_size = sizeof a / sizeof *a;
int target = 18;
// Declares the array in main.
int indices[2];
if ( find_indices_of_sum(a_size, a, target, indices) )
{ // Pass the address of its first element ^^^^^^^, the size is known (2).
printf("Target found at indices %d and %d\n", indices[0], indices[1]);
// ^^
// No need to free anything. 'indices' has automatic storage duration.
}
else
{
puts("Target not found");
}
return 0;
}
We can also directly return a struct with all the needed informations from the function, without using any "out parameter".
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct idx_pair
{
size_t first, second;
};
static inline bool is_valid(struct idx_pair p)
{
return p.first != p.second;
}
// The function returns both the indices wrapped in a single struct.
struct idx_pair
find_inidces_of_sum( size_t n, int const *arr
, int target )
{
for(size_t i = 0; i < n; ++i)
{
for( size_t j = i + 1; j < n; ++j)
{
if( arr[i] + arr[j] == target )
{
return (struct idx_pair){ .first = i, .second = j };
// ^^^^^^^^^^^^^^^^^ This is not a cast, it's a
// compound literal with designated initializers.
}
}
}
return (struct idx_pair){ .first = 0, .second = 0 };
}
int main(void)
{
int a[] = {2, 7, 11, 15};
size_t a_size = sizeof a / sizeof *a;
int target = 18;
struct idx_pair solution = find_indices_of_sum(a_size, a, target);
if ( is_valid(solution) )
{
printf("Target found at indices %d and %d\n", solution.first, solution.second);
}
else
{
puts("Target not found");
}
return 0;
}
Note though, that your compiler might not be able to optimize out all the copies and, even if the involved structure isn't that big, the generated assembly might be not optimal on some hardware.
I have a task of creating a matrix, that is the size of NxN, where N is a given parameter.
The matrix should be filled with random 0s and 1s.
I have tried the following code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <sys/wait.h>
#include <pthread.h>
int** createMatr(int N){
int** matr[10][10];
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
int rnd = rand() % 2;
matr[i][j] = rnd;
}
}
return matr;
}
void setValMatr(int N, int** matr[][10], int** newMatr[][10]){
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
newMatr[i][j] = matr[i][j];
}
}
}
void printMatr(int N, int** matr[][10]){
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%d ",matr[i][j]);
}
printf("\n");
}
}
int main(int argc, char* argv[]){
if(argc!=2){
perror("parameter error\n");
}
int N = atoi(argv[1]);
int** matrix[10][10];
int** helper[10][10];
setValMatr(N,createMatr(N), matrix);
setValMatr(N,matrix,helper);
printMatr(N, matrix);
return 0;
}
The compilation warnings this gives me are:
┌──(kali㉿kali)-[~/Desktop/Cprog/Linux2_Lab2]
└─$ gcc gvim2135_L2_1.c -o p
gvim2135_L2_1.c: In function ‘createMatr’:
gvim2135_L2_1.c:15:24: warning: assignment to ‘int **’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
15 | matr[i][j] = rnd;
| ^
gvim2135_L2_1.c:18:12: warning: returning ‘int ** (*)[10]’ from a function with incompatible return type ‘int **’ [-Wincompatible-pointer-types]
18 | return matr;
| ^~~~
gvim2135_L2_1.c:18:12: warning: function returns address of local variable [-Wreturn-local-addr]
gvim2135_L2_1.c: In function ‘main’:
gvim2135_L2_1.c:47:18: warning: passing argument 2 of ‘setValMatr’ from incompatible pointer type [-Wincompatible-pointer-types]
47 | setValMatr(N,createMatr(N), matrix);
| ^~~~~~~~~~~~~
| |
| int **
gvim2135_L2_1.c:21:30: note: expected ‘int ** (*)[10]’ but argument is of type ‘int **’
21 | void setValMatr(int N, int** matr[][10], int** newMatr[][10]){
| ~~~~~~^~~~~~~~~~
After running I get the error:
Segmentation fault
With the int** matrix[][] notation you are not create a matrix but a matrix of matrix pointer. You can see a matrix as a pointer to an array of array: int** matrix.
So, your code become:
int** create_matrix(int size) {
int i, j, **matrix = (int **)malloc(size * sizeof(int*));
for (i = 0; i < size; ++i) {
matrix[i] = (int *)malloc(size * sizeof(int));
for (j = 0; j < size; ++j) {
matrix[i][j] = rand() % 2;
}
}
return matrix;
}
void print_matrix(int** matrix, int size) {
int i, j;
for (i = 0; i < size; ++i) {
for (j = 0; j < size; ++j) {
printf("%d | ", matrix[i][j]);
}
printf("\n");
}
}
Use a structure to describe matrices in general, for example
typedef struct imatrix imatrix;
struct imatrix {
int rows;
int cols;
int *data;
};
static inline int imatrix_get(const imatrix *im, int row, int col, int outside)
{
if (!im || row < 0 || col < 0 || row >= im->rows || col >= im->cols)
return outside;
else
return im->data[row * (size_t)(im->cols) + col];
}
static inline void imatrix_set(imatrix *im, int row, int col, int value)
{
if (im && row >= 0 && col >= 0 && row < im->rows && col < im->cols)
im->data[row * (size_t)(im->cols) + col] = value;
}
void imatrix_free(imatrix *im)
{
if (im) {
free(im->data);
im->rows = 0;
im->cols = 0;
im->data = NULL;
}
}
int imatrix_new(imatrix *im, int rows, int cols)
{
if (!im)
return -1; /* No matrix specified */
im->rows = 0;
im->cols = 0;
im->data = NULL;
if (rows < 1 || cols < 1)
return -2; /* Invalid size */
const size_t rowsize = (size_t)cols * sizeof im->data[0];
const size_t datasize = (size_t)rows * rowsize;
if ((size_t)(datasize / rows) != rowsize ||
(size_t)(rowsize / cols) != sizeof im->data[0])
return -3; /* Matrix is too large */
im->data = malloc(datasize);
if (!im->data)
return -4; /* Not enough memory available */
im->rows = rows;
im->cols = cols;
return 0;
}
The idea is that imatrix is a structure that contains the number of rows and columns in the matrix, and a pointer to the (array of) data elements. If m is a properly initialized imatrix, element at row r, column c is m->data[r*(size_t)(m->cols) + c].
Note that the int type is not large enough on many architectures to for the index, so we need to cast the number of columns to the proper type, size_t (which is the proper type for all in-memory sizes and offsets).
If you have e.g. imatrix m;, you need to initialize it (and dynamically allocate memory for it, say 30 rows and 20 columns) by calling imatrix_new(&m, 30, 20);. It will return 0 if successful, and negative error code if it fails.
The imatrix_get() and imatrix_set() functions are convenient accessor functions, that will not try to access the matrix contents outside of bounds. You do not need to use them in your own functions, if you make sure your loops etc. are always within range; you can then safely use the im->data[r * (size_t)(im->cols) + c] idiom.
When you no longer need the matrix, you discard it with a simple imatrix_free(&m) call.
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];
I'm trying to multiply a large and random matrix (NxN) and a random vector (N), using pointers.
Why am i getting an error type "invalid operands to binary * (have 'double *' and 'double *')" ?
The error seems to be in ptr3[i][j] = ptr3[i] + ptr1[i] * ptr2[k];
but I can't figure out why this doesn't work.
I'm new to C, so I still don't get pointers very well.
int main ()
{
time_t t;
double **ptr1, **ptr2, **ptr3;
int i, j, k;
int N = 500;
ptr1 = (double **) malloc (sizeof (double *) * N);
ptr2 = (double **) malloc (sizeof (double *) * N);
ptr3 = (double **) malloc (sizeof (double *) * N);
for (i = 0; i < N; i++)
ptr1[i] = (double *) malloc (sizeof (double) * N);
for (i = 0; i < N; i++)
ptr2[i] = (double *) malloc (sizeof (double) * N);
for (i = 0; i < N; i++)
ptr3[i] = (double *) malloc (sizeof (double) * N);
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
ptr1[i][j] = rand ();
}
}
for (i = 0; i < N; i++) {
*ptr2[i] = rand ();
}
t = clock();
for (i = 0; i < N; i++) {
ptr3[i] = 0;
for (k = 0; k < N; k++)
ptr3[i] = ptr3[i] + ptr1[i][k] * ptr2[k];
}
t = clock() - t;
double time_taken = ((double)t)/CLOCKS_PER_SEC;
printf("Tempo: %f segundos \n", time_taken);
printf ("\n");
return (0);
} ```
What the compiler is saying is that in the statement ptr3[i] = ptr3[i] + ptr1[i] * ptr2[k];, the bit which says ptr1[i] * ptr2[k] is attempting to * between two expressions which have the type double*. In other words, you are not allowed to multiply two pointers together. To do this properly, you need to dereference again (the [i] and [k] are dereferencing the double** to a double* already). To get this to compile, that statement should be (I added parenthesis for clarity -- they aren't actually needed):
*ptr3[i] = (*ptr3[i]) + (*ptr1[i]) * (*ptr2[k]);
That should get you to compiling, but the next problem you'll run into is a segmentation fault. Two lines above the place where you do the multiplication, you have this:
ptr3[i] = 0;
This is assigning ptr3[i] to be the null pointer, which is the same as 0 in C (other languages have a different name of this value: null, None, etc). What I think you meant to do here is:
*ptr3[i] = 0;
As an aside, since N is a known, fixed value, you can choose to not deal with all the malloc stuff by simply saying:
const int N = 500;
double ptr1[N][N];
double ptr2[N][N];
// ... and so on ...
This is declaring ptr1 as an array instead of a pointer, which is identical to a pointer in terms of memory access patterns, but different in a number of ways. Depending on what you are trying to learn, not dealing with dynamic memory (using malloc and free) might save you a little bit of headache for now.
I have the following warnings during the compilation:
solver.c:24: warning: passing argument 2 of ‘mtrx_multiple’ from incompatible pointer type
mat.h:5: note: expected ‘double *’ but argument is of type ‘double **’
solver.c:30: warning: assignment makes pointer from integer without a cast
solver.c:39: warning: assignment makes pointer from integer without a cast
/tmp/ccmU9zRf.o: In function `vec_norm':
math.c:(.text+0x331): undefined reference to `sqrt'
collect2: ld returned 1 exit status
the lines are:
solver.c
double *cg_solve( sparse_mat_t A, double *b, double *x ) {
double *a;
double **r;
double *be;
double **p;
double **x0;
x0[0] = vec_copy(x, size);
...
line 24: r[0] = vec_subtraction( b, mtrx_multiple(A, x0), size );
line 30: x0[k+1] = vec_addition( x0[k], vec_numb_multiple(a[k], p[k], size), size );
line 39: p[k+1] = vec_addition( r[k+1], vec_numb_multiple(be[k], p[k], size), size );
}
math.h
line 5: double *mtrx_multiple (sparse_mat_t A, double *c);
The function that are used there: (math.c)
double *vec_subtraction (double *a, double *b, int n) {
double *result = malloc(sizeof(double)*n);
int i;
for(i=0; i<n; i++)
result[i] = a[i]-b[i];
return result;
}
double *vec_addition (double *a, double *b, int n) {
double *result = malloc(sizeof(double)*n);
int i;
for(i=0; i<n; i++)
result[i] = a[i]+b[i];
return result;
}
double *vec_numb_multiple (double a, double *b, int n) {
double *result = malloc(sizeof(double)*n);
int i;
for(i=0; i<n; i++)
result[i] = a*b[i];
return result;
}
double *mtrx_multiple (sparse_mat_t A, double *c) {
double *result;
int i, j;
result = malloc((A.size) * sizeof *result);
printf("c.n: %d \n", A.size);
for (i = 0; i < A.size; i++) {
int v = 0;
for (j = A.ia[i]; j < A.ia[i + 1]; j++) {
v += A.a[j] * c[A.ja[j]];
}
result[i] = v;
}
return result;
}
double vec_norm (double *a, int n){
double result;
int i;
for(i=0; i<n; i++)
result = result + ( a[i] * a[i] );
result = sqrt(result);
return result;
}
double *vec_copy (double *a, int n) {
double *result;
int i;
for(i=0; i<n; i++)
result[i] = a[i];
return result;
}
I will be grateful for any help.
EDIT
I found the solution to the x0 problem, thanks Ben. Now what left is:
solver.c:30: warning: assignment makes pointer from integer without a cast
solver.c:39: warning: assignment makes pointer from integer without a cast
/tmp/ccL4uSoH.o: In function 'vec_norm':
math.c:(.text+0x331): undefined reference to 'sqrt'
collect2: ld returned 1 exit status
Based on what you've posted, I'm going to guess that you don't have a declaration for vec_numb_multiple in scope before you call it, and the compiler is implicitly typing it to return int; that would lead to the warnings on lines 30 and 39.
The undefined reference to sqrt() means you aren't linking in the standard math library; I'm assuming you're using gcc, so you would need to add -lm to the command line.
It's a really bad idea to use a standard library file name for your own code (math.h, math.c).
replace (line 24)
r[0] = vec_subtraction( b, mtrx_multiple(A, x0), size );
with
r[0] = vec_subtraction( b, mtrx_multiple(A, x0[0]), size );
You said you whant to multiply a matrix (A I guess) with a vector, so the second argument must be a vector. x0 is a pointer to pointers which can be see as a 2D array of doubles, it means a single cell of x0 is an array of doubles (ie. what you could call a vector). This is why you want to pass x0[0], not just x0 which is : many arrays.
see John's aswer for the rest.