Segmentation fault on memcmp [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
int cauta(const void *x, int n, int dim_el, const void *el)
{
char *c = (char*) x;
int i;
for(i = 0; i < n; i++)
{
if(memcmp(c + i * dim_el, el, dim_el) == 0)
return 1;
}
return -1;
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int k;
k = cauta(a, sizeof(a) / sizeof(a[0]), sizeof(a[0]), a[0]);
printf("%d", k);
return 0;
}
The problem appears on the commented line. The function returns 1 if "el" exists in the "x" array . It's a simple, yet I don't understand exactly why it's a segmfault.
Also, this is the call stack display when I tried debugging it line by line.

In your code, the function parameters are
int cauta(const void *x,int n,int dim_el,const void *el)
where el expects a const void *, whereas, while calling,
cauta(a,sizeof(a)/sizeof(a[0]),sizeof(a[0]),a[0]);
you passed a[0] which is an int.
You need to pass an address, like &a[3], for example.
That said, int main() should be int main(void) to conform to the standards.

You're passing in an int i.e. a[0] into cauta as an const void * that's going to cause the error.

Related

My program returns 4 instead of expected value 40 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
#include <stdio.h>
int value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
int value(int *a){
int c = (*a)*10;
return c;
}
In this code, I transfer the address in function but it does not change, Why?
You have 2 ways of having a function change data outside it: you can pass a variable by reference and update it, or you can return a value and use that. You are mixing half of each method instead. I reordered the code to make my commentary clearer.
#include <stdio.h>
int value(int *a);
// here you are passing by reference, good
int value(int *a){
int c = (*a)*10; // but you don't change a
return c; // instead you return a new value
}
int main(){
int num = 4;
value(&num); // but here you ignore the new value.
printf("value of number is = %d", num);
return 0;
}
to make reference way work, you need:
#include <stdio.h>
void value(int *a);
int main(){
int num = 4;
value(&num);
printf("value of number is = %d", num);
return 0;
}
void value(int *a){
*a = (*a)*10; // change the value that a points at
// no need to return anything
}
or to make the return way work:
#include <stdio.h>
// no need to pass by reference
int value(int a);
int main(){
int num = 4;
num = value(num); // use value return by function
printf("value of number is = %d", num);
return 0;
}
int value(int a){
int c = a * 10;
return c;
}

How to print result of function in C using pointer (w/o return) [duplicate]

This question already has answers here:
C input and out parameters of a function
(3 answers)
how to set a int value passed by parameter to a function and assign it to global so I can use outside of the function?
(3 answers)
Closed 4 years ago.
My task is to write a function which calculates sum of elements of array. which I did like so:
#include <stdio.h>
#include <stdlib.h>
int sum (int array[], int len){
int i;
int sum=0;
for (i=0; i<len; i++){
sum = sum + array[i];
}
return sum;
}
int main() {
int array[] = {9, 4, 7, 8, 10, 5, 1, 6, 3, 2};
int len = 10;
printf ("Sum: %d\n" , sum(array, len));
}
Output: Sum: 55
however, now I need to do the very same thing but differently. The Sum function should take 3 arguments, third one being a Pointer, return no value and still be able to call it in the main function to print out the sum again.
Any help would be appreciated.
P.S. Yes, it is part of homework.
The pointer will point to the integer variable that will receive the sum:
void sum (int array[], int len, int *result)
You call it from main giving a pointer to the result. I give no more; the rest is your homework.

C calculating factorials using functions [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
So i tried to code a function to output nCr (the combinations of choosing k elements from n elements) but it does not show any output...
I think I am unable to call the function correctly but I think my syntax is correct:
#include <stdio.h>
int factorial( int n)
{
int i, nff, nf[10];
for(i=0;i<n;i++)
nf[i]=(n-i);
for(i=0;i<n-1;i++)
nf[i+1]*=nf[i];
nff=nf[n-1];
return nff;
}
int faktorial( int k){
int i, kff, kf[10];
for(i=0;i<k;i++)
kf[i]=(k-i);
for(i=0;i<k-1;i++)
kf[i+1]*=kf[i];
kff=kf[k-1];
return kff;
}
int facktorial( int k, int n){
int i, nkff, nkf[10];
for(i=0;i<(n-k);i++)
nkf[i]=(n-k)-i;
for(i=0;i<(n-k)-1;i++)
nkf[i+1]*=nkf[i];
nkff=nkf[(n-k)-1];
return nkff;
}
int combination( int k, int n)
{
// this function shall call (make use of) another function factorial()
int nfa,kfa,nkfa,nCra;
nfa=factorial(n);
kfa=faktorial(k);
nkfa=facktorial(k,n);
nCra = nfa/(kfa*nkfa);
return nCra;
}
int main(void)
{
int n, k, nCr;
scanf("%d %d", &n, &k);
nCr=combination (k, n);
return 0;
}
You just need to output the result when it is returned:
printf("%d\n", nCr);
return 0;
Another issue, your program will crash if the input is 0 or a number greater than 10, it is better not to use an array to computer factorial.

C Arrays gives address instead of value when second array is created [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
First of all, sorry for my English. I hope you will still understand what my problem is.
I am new to C programming and I am a bit confused. Here is my code.
#include <stdio.h>
#include "tableaux.h"
int main(int argc, const char * argv[]) {
int tableauUn[4] = {1, 1, 1, 1};
printf("%d\n", sommeTableau(tableauUn, 4));
printf("%d\n", moyenneTableau(tableauUn, 4));
return 0;
}
And this is the file where my functions are. I also have a file where I have my prototypes.
#include <stdio.h>
#include "tableaux.h"
int sommeTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat;
}
int moyenneTableau(int tableau[], int taille) {
int resultat;
for (int i = 0; i < taille; i++) {
resultat += tableau[i];
}
return resultat / taille;
}
void copierTableau(int tableau[], int taille, int tableauDeux[]) {
for (int i = 0; i < taille; i++) {
tableauDeux[i] = tableau[i];
}
}
So everything works fine. The first printf gives me the total of the values that are stored in the array and the second one gives me the average of the values.
5
1
Program ended with exit code: 0
What I don't understand is why do I get this result when I want to create a second array ?
int tableauUn[4] = {1, 1, 1, 1};
int tableauDeux[4] = {0};
the result
1606416356
1
Program ended with exit code: 0
So I haven't used the second array but the result of the first printf changes and I am a bit confused with what is going on.
I hope you can help me !
Please initialize the sum you return in function
int resultat = 0;
otherwise resultat will take garbage value as initial value

2d arrays with pointers [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I have the following code so far:
#include <stdio.h>
int foo (int *pointer, int row, int col);
int main() {
int array[3][3] ={ {1,2,3},{4,5,6},{7,8,9}};
int *pointer= array[0];
int row = 2;
int col = 2;
int answer = foo (pointer, row, col);
printf("%d", answer); //prints 5 (which is wrong)
printf("%d", array[2][2]); //prints 9
}
int foo (int *pointer, int row, int col){ //I don't want to use any square brackets here, unless I really have to.
int value;
value = *((int *)(pointer+row)+col);
return value;
}
So my main issue is with passing a 2D pointer, please explain in detail as I am still new at coding. I don't want to really change what I am passing (as in I want to use the pointer in foo(pointer, row, col) and not foo (array, row, col).
passing a 2D pointer
From how you (ab)used the terminology, it's quite clear that you're under the wrong impression that pointers and arrays are the same. They aren't.
If you want to access a multidimensional array using pointers, you must specify all its dimensions (except the first, innermost one) and use pointer arithmetic correctly, possibly using pointers-to-array, since multidimensional arrays are contiguous in memory:
const size_t h = 2;
const size_t w = 3;
int arr[h][w] = {
{ 1, 2, 3 },
{ 4, 5, 6 }
};
int *p = &arr[1][2];
int *q = arr[1];
int (*t)[3] = &arr[1];
printf("arr[1][2] == %d == %d == %d\n", *p, q[2], (*t)[2]);
return 0;
int *pointer= array[0];
Instead of use this
int *pointer= &array[0];
Or
int *pointer= array;

Resources