When ever i run this code, it works up until i get to the printf statement in my main function, thats when i get a segmentation fault error. so it will run like "enter how many numbers you want" 3 "Enter the numbers in the array" 1 2 3 array[0] = 1 array[1] = 2 array[2] = 3 segmentation error. Can you guys please tell me why im getting this error and how to fix it? thank you
//pathfinder.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Vector.h"
int main()
{
Vector *V;
VectorRead(V);
printf("%d", V->item[0]);
return 0;
}
//Vector.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
typedef struct{
int *item;
int size;
} Vector;
void VectorRead(Vector *V) ;
void VectorRead(Vector *V)
{
int N;
printf("Enter how many numbers you want?\n");
scanf("%d", &N);
V = (Vector *)malloc(sizeof(Vector *) * N);
V->size = N;
V->item = (int *)malloc(sizeof(int *) * V->size);
printf("Enter the numbers that you want in your array\n");
int i = 0;
while(i < V->size && scanf("%d", &(V->item[i++])) == 1);
int j;
for(j = 0; j< V->size; j++){
printf("array[%d]=%d\n", j, V->item[j]);
}
}
This error has nothing to do with your code being in different files.
When you call VectorRead(), you are passing a pointer value. Inside that function, you are setting the local V to the return value of a call to malloc(). There is no way for that local V to return back to the caller.
You will need to do something to return the newly allocated value of V back to the caller. Changing your function to return a Vector * (instead of taking one as a parameter) would be a good approach.
Your local Vector, V, is not being modified when VectorRead() is called. Try instead accepting a Vector ** in your function:
void VectorRead(Vector **V)
and modify the function accordingly.
Or, since your function has no return value, and as #EOF points out in the comments, it is probably a better idea to not take a parameter, and simply return the Vector *:
Vector *VectorRead(void)
Related
so I'm trying to make a 2d binary matrix that is the size provided by stdin and that has randomly assigned indexes for the 0 and 1, however, their cannot be more than size/2 zeroes or ones.
For example
an input of 2
could output
1 0
0 1
Now I was going to originally just use the argument int arr[][n] in init but this idea failed since passing in the matrix just resulted in my program going on some sort of an infinite loop when I attempted to access matrix again inside of the main function. I believe this happened because the lifespan of matrix expired when init concluded? So my question here is why is what I'm doing now producing the below error and how can I fix this up?
note: expected ‘int * (*)[(sizetype)(n)]’ but argument is of type ‘int (*)[(sizetype)(dim)][(sizetype)(dim)]’
7 | int init(int n, int* arr[][n]);
My code
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int init(int n, int* arr[][n]);
int main(){
time_t t;
srand((unsigned) time(&t));
int dim;
printf("Enter a positive even integer: ");
scanf("%d",&dim);
if(dim<2||dim>80){
return 1;
}
int matrix[dim][dim];
init(dim,&matrix);
return 0;
}
int init(int n, int* arr[][n]){
int numZeroes,numOnes;
int zero_or_one;
for(int row=0;row<n;row++){
numZeroes=0;
numOnes=0;
for(int col=0;col<n;col++){
if(numZeroes<n/2 && numOnes<n/2){
zero_or_one=rand()%2;
*arr[row][col]=zero_or_one;
if(zero_or_one==1){
numOnes++;
}
if(zero_or_one==0){
numZeroes++;
}
}
else{
if(numZeroes==n/2 && numOnes<n/2){
*arr[row][col]=1;
}
if(numZeroes<n/2 && numOnes==n/2){
*arr[row][col]=0;
}
}
}
}
for(int row=0;row<n;row++){
for(int col=0;col<n;col++){
printf("%d ",*arr[row][col]);
}
printf("\n");
}
return 0;
}
The function should be declared like
void init(int n, int arr[][n]);
(the return type int of the function does not make a great sense.)
and called like
init(dim, matrix);
Within the function instead of statements like this
*arr[row][col]=zero_or_one;
you have to write
arr[row][col]=zero_or_one;
I am making this program in which my main function calls a function which returns an array after the calculation. I checked already that calculation is right inside the local function. But when I return that array to 'main' function then I only can print the correct value one time and it prints wrong value all other times.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int* getJoinedPipes(int input1_size, int* input1,int* output_size){
int i,j,temp;
int op1[input1_size-1];
*output_size = input1_size - 1;
for(i=0; i<input1_size; i++){
for(j=i+1; j<input1_size; j++){
if(input1[i] > input1[j]){
temp = input1[i];
input1[i] = input1[j];
input1[j] = temp;
}
}
}
op1[0]=input1[0] + input1[1];
for(i=1;i<input1_size-1;i++){
op1[i] = op1[i-1]+input1[i+1];
}
//printf("%d\n",op1[2]);
return op1;
}
int main() {
int output_size;
int* output;
int ip1_size = 0;
int ip1_i;
scanf("%d\n", &ip1_size);
int ip1[ip1_size];
for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
int ip1_item;
scanf("%d", &ip1_item);
ip1[ip1_i] = ip1_item;
}
output = getJoinedPipes(ip1_size,ip1,&output_size);
printf("a==%d\n",output[0]);
printf("a==%d\n",output[0]);
printf("a==%d\n",output[0]);
int output_i;
for(output_i=0; output_i < output_size; output_i++) {
printf("%d\n", output[output_i]);
}
return 0;
}
Output should be
5
9
15
But in the console, it's showing the following (after dry run).
a==5
a==1943372821
a==1943372821
1943372821
17
6356632
You can see first time its giving correct value (5) and later for same print its giving garbage values.
op1 is an automatic array. You cannot return it and use it outside its scope.
op1 exists only within getJoinedPipes, if you return it, the result is Undefined Behaviour.
To fix it you can either:
pass op1 as a parameter to getJoinedPiped
allocate op1 on the heap dynamically. You you do that, you can safely return op1 but you have to remember to free it when you don't need it.
You are returning pointer to a local variable object whose lifetime has ended yet when you are trying to get its values -> horrible mistake and undefined behaviour.
If you want to return an array from function, allocate it dynamically via malloc, and dont forget to free it after you are done with that array. You should also check return value of malloc if you got memory you asked for.
Correct
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int* getJoinedPipes(int input1_size, int* input1,int* output_size){
int i,j,temp;
int * op1 = (int *)malloc(sizeof(int) * (input1_size-1));
*output_size = input1_size - 1;
for(i=0; i<input1_size; i++){
for(j=i+1; j<input1_size; j++){
if(input1[i] > input1[j]){
temp = input1[i];
input1[i] = input1[j];
input1[j] = temp;
}
}
}
op1[0]=input1[0] + input1[1];
for(i=1;i<input1_size-1;i++){
op1[i] = op1[i-1]+input1[i+1];
}
//printf("%d\n",op1[2]);
return op1;
}
int main() {
int output_size;
int* output;
int ip1_size = 0;
int ip1_i;
scanf("%d\n", &ip1_size);
int ip1[ip1_size];
for(ip1_i = 0; ip1_i < ip1_size; ip1_i++) {
int ip1_item;
scanf("%d", &ip1_item);
ip1[ip1_i] = ip1_item;
}
output = getJoinedPipes(ip1_size,ip1,&output_size);
printf("a==%d\n",output[0]);
printf("a==%d\n",output[0]);
printf("a==%d\n",output[0]);
int output_i;
for(output_i=0; output_i < output_size; output_i++) {
printf("%d\n", output[output_i]);
}
free(output);
return 0;
}
You should check your indentation... apart from that, I guess the problem could arise because the array you output in your function is created on the function stack. So what you get as an output array is a reference on the stack of your joinedPipes-function. Try to pass your array as an argument to the function and not create it as a return value.
Hope that does the trick...
I'm learning C.I wrote code and error is "passing argument 1 of 'strcpy' makes pointer from integer without a cast".
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct humans{
sname[20];
}human;
int main(){
human *person=(human *)malloc(sizeof(human)*1);
int i,k,z;
for(i=0;i<5;i++){
person=(human *)realloc(person,sizeof(human)*(i+1));
strcpy(*person[i].sname , "john");
}
for(i=0;i<5;i++){
printf("%s",*person[i].sname);
}
return 0;
}
I want to use malloc/realloc.
Where you have *person[i].sname, you want person[i].sname instead. When you put * before it, you force the array to decay to a pointer to its first element which, when dereferenced, gives you the value of the first element.
Also:
person=(human *)realloc(person,sizeof(human)*1);
This 1 should be i + 1.
I fixed the direct issues in your program.
The deeper point here though is how to get to the pointer you want.
array[4] gets the value of the 4th element.
array gets the pointer to the first element
&array[4] gets the pointer to the 4th element
array + 4 gets the pointer to the 4th element
*(array + 4) gets the value of the 4th element
*array[4] gets the value, treats it as a pointer and tries to get a value from the pointers target - this would require extra fiddling to convince the compiler what type that would be. In most cases this is likely meaningless.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct humans {
char sname[20];
}human;
void MyStrCpy(char * dst, int dst_n, char * src, int src_n)
{
for (int n = 0; n < dst_n && n < src_n; ++n)
{
dst[n] = src[n];
if (src[n] == 0) break;
}
dst[dst_n] = 0;
}
int main() {
human *person = (human *)malloc(sizeof(human) * 1);
int i, k, z;
for (i = 0; i<5; i++) {
person = (human *)realloc(person, sizeof(human)*(i + 1));
//I replace strcpy so it compiles on my machine
MyStrCpy(person[i].sname, 20, "john", 5);
person[i].sname[20] = 0;
}
for (i = 0; i<5; i++) {
printf("%s\n", person[i].sname);
}
return 0;
}
The code runs until it reaches the statement:
printf("%d", sumOccur(input));
The code:
#include <stdio.h>
#include <stdlib.h>
int sumOccur(int A[]);
int main(){
int input[6] = {1,1,1,2,2,3};
printf("%d", sumOccur(input));
return 0;
}
int sumOccur(int A[]) {
int sum, i;
while(A[i]!='\0'){
sum += A[i];
i++;
}
return sum;
}
If I have made any silly mistakes please oblige.
It's not the printf() crashing. It's sumOccur(). Your array has no \0 value in it, so your while() never terminates and you end up in a near-infinite loop and run off the end of the array.
The array is an array of numbers, not a string, so there is no reason whatsoever to think there there would be a null-terminator on the values. null terminators are for strings, not arrays of numbers.
In your function int sumOccur you have two problems-
1. sum and i are not initialized just declared. Initialize both to 0 .
2. Also while(A[i]!='\0') ain't going to work as expected as your array doesn't have that value in it.
Your code invokes undefined behaviour: you access A[6] and subsequent inexistent entries in sumOccur trying to find a final 0 in the array, but you do not put one in the definition of input in the main function.
-------- cut here if you are not interested in gory implementation details --------
The array is allocated on the stack, very near the top since it is instantiated in the main function. Reading beyond the end until you find a 0 likely tries to read beyond the end of the stack pages and causes a segmentation fault.
Note that you are dealing with an int array,which means it normally won't contain '\0' character.To iterate over the array you need to specify number of elements.Here is the correct way :
#include <stdio.h>
#include <stdlib.h>
int sumOccur(int A[],size_t number_of_elemets);
int main(){
int input[6] = {1,1,1,2,2,3};
//Get the number of elements
size_t n = sizeof(input) / sizeof(int);
printf("%d", sumOccur(input,n));
return 0;
}
int sumOccur(int A[],size_t number_of_elements) {
int sum = 0;
size_t i = 0;
while( i < number_of_elements )
{
sum += A[i];
i++;
}
return sum;
}
You are iterating while A[i] != '\0' but there is no '\0' in the array and also you never initialize sum which is unlikely the cause for a crash but it could be.
You need to pass the number of elements in the array, like this
#include <stdio.h>
#include <stdlib.h>
int sumOccur(size_t count, const int *A);
int sumOccurCHQrlieWay(const int *A, size_t count);
int main()
{
int input[] = {1, 1, 1, 2, 2, 3};
printf("%d", sumOccur(sizeof(input) / sizeof(*input), input));
return 0;
}
int sumOccur(size_t count, const int *A)
{
int sum;
sum = 0;
for (size_t i = 0 ; i < count ; ++i)
sum += A[i];
return sum;
}
int sumOccurCHQrlieWay(const int *A, size_t count)
{
return sumOccur(count, A);
}
im a 1st grader when it comes to c and need help with storing 5 random values in an array and outputting them. Heres where am at.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct score_card {int A_ones; int B_twos; int C_threes; int D_fours; int E_fives; int F_sixes; int G_chance;};
int dice_rolls[5];
int randomize(void);
int value;
int main(void) {
struct score_card test;
randomize;
int i;
for(i = 0; i <= 4; i++){
printf("%d\n", dice_rolls[i]);
}
printf("\n");
return 0;
}
int randomize(void){
int i;
srand(time(0));
for(i = 0; i <= 4; i++){
value = rand() % 6 + 1;
dice_rolls[i] = value;
}
}
The output is :
6294304
6294308
6294312
6294316
6294320
the goal was to use modular division to get values from 1 -->6 and store them in the dicerolls array.
I see two immediate problems.
First. you're not terminating your random numbers with a newline. That's why they're all strung together in a big sequence. Change your output line to:
printf("%d\n", &dice_rolls[i]);
Secondly, you're not actually calling randomize. The correct way to call it is with:
randomize();
The statement randomize; is simply an expression giving you the address of the function. It's as useless in this case as the expression 42; which also does nothing. However it's valid C so the compiler doesn't necessarily complain.