Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
float** getdata(void);
void calculations(float* fPtr);
//void printoriginal(float* values, int inputnum, float* fPtr);
int main(void)
{
float** fPtr;
float* values;
fPtr = getdata();
calculations(*fPtr);
int element;
// printoriginal(*values, inputnum, *fPtr);
system("PAUSE");
return 0;
}
float getvalues(void)
{
float* values = (float*)calloc(*inputnum, sizeof(float));
float** getdata(void)
{
int i;
int n;
float** fPtr;
int* inputnum;
printf("How many values do you want to input into the array?");
scanf("%d", inputnum);
float* values = (float*)calloc(*inputnum, sizeof(float));
if (values == NULL)
{ printf("Memory overflow\n");
exit(101);
}
for(i = 0; i < *inputnum; i++)
{
n = i + 1;
printf("Please enter your %d number: ", n);
scanf("%f",(values+i));
}
fPtr = (float**)calloc(*inputnum+1, sizeof(float*));
if (fPtr == NULL)
{ printf("Memory overflow\n");
exit(101);
}
for(int c=0; c < *inputnum; c++)
{
*(fPtr+i) = (values+i);
}
printf("%f", values+2);
return fPtr;
}
I scanf the values in getdata, but I am scanning in garbage. Im pretty certain my scanf is off. Also, how would I be able to pass back my values array through reference?? I am having a very hard time with that as well. Thanks everyone.
Instead of
int * inputnum;
scanf( ..., inputnum );
you want:
int inputnum;
scanf( ..., &inputnum );
Either that, or you need to insert an allocation of inputnum:
/* Very non-C-worthy code sample */
int * inputnum;
inputnum = malloc( sizeof *inputnum );
if( inputnum == NULL ) {
perror( "malloc" );
exit( EXIT_FAILURE );
}
scanf( ..., inputnum );
but that will look very strange to anyone familiar with C
(Unless there is some other reason for it, and in this
case there does not appear to be any reason.)
The problem you are experiencing is that the scanf is
storing the received value in the location pointed to
by inputnum, which is a bogus location since inputnum
is uninitialized.
I like to have objects "assigned" to one single function. In your program, your values are created in the function getdata(), used in calculations() and never released.
If the main() function was responsible for them, it would make coding easier, in my opinion. Something like
int main(void) {
int inputnum, innum;
double *fPtr;
printf("How many values do you want to input into the array?");
fflush(stdout);
if (scanf("%d", &inputnum) != 1) {
fprintf(stderr, "Error in input. Program aborted.\n");
exit(EXIT_FAILURE);
}
if (inputnum > MAX) { /* ... nothing yet ... */ }
fPtr = calloc(inputnum, sizeof *fPtr);
if (fPtr == NULL) {
fprintf(stderr, "Not enough memory. Program aborted.\n");
exit(EXIT_FAILURE);
}
/* use fPtr in other functions */
innum = data_get(fPtr, inputnum);
data_print(fPtr, innum);
free(fPtr);
return 0;
}
In the snippet above, I used the functions data_get() and data_print() with a somewhat different prototype than you have. These functions take a pointer and a number of elements.
int data_get(double *data, int num);
void data_print(double *data, int num);
The first one reads up to num doubles into the memory pointed to by data and returns the number of doubles that was effectively entered.
The second takes number of doubles to print.
Oh! Unless you have a very good reason to use floats, don't use them. Always prefer doubles.
Also Instead of
printf("%f", values+2);
use
printf("%f", *(values+2));
There is so many errors, I didn't know where to start :)
Code completely rewritten below. Please study this.
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
int get_data (float** array); /* must be pointer-to-pointer to return through parameter */
void print_data (const float* array, int size); /* print data */
void do_weird_things (float* array, int size); /* do weird things with the data */
void clear_data (float* array); /* clean up */
int main(void)
{
float* array;
int size;
size = get_data (&array);
printf("Before weird things:\n");
print_data (array, size);
do_weird_things (array, size);
printf("After weird things:\n");
print_data (array, size);
clear_data (array);
system("PAUSE");
return 0;
}
int get_data (float** array)
{
int i;
int items;
printf("How many values do you want to input into the array? ");
scanf("%d", &items);
getchar(); /* remove \n from stdin */
*array = calloc(items, sizeof(float));
if (*array == NULL)
{
printf("Memory overflow\n");
exit(EXIT_FAILURE);
}
for(i = 0; i < items; i++)
{
printf("Please enter your %d number: ", i+1);
scanf("%f", (*array) + i);
getchar(); /* remove \n from stdin */
}
return items;
}
void do_weird_things (float* array, int size) /* do whatever data manipulation you wish here */
{
int i;
for(i=0; i<size; i++)
{
array[i] += i;
}
}
void print_data (const float* array, int size)
{
int i;
for(i=0; i<size; i++)
{
printf("%f\t", array[i]);
}
printf("\n");
}
void clear_data (float* array)
{
free(array);
}
Related
I am new to C and I was trying to code up a dynamic int array that lets the user type in numbers via terminal untill the user types in -1. So the user can type any amount of int's and the data is reallocated each time another valid int is input.
Sometimes it works as expected other times I get: free(): double free detected in tcache 2
Aborted (core dumped) error.
I am printing the values after every input and everything seems fine untill -1 is input. And the bug only occures every other time.
Here is my Code:
void output(int *ptr, int len){
printf("\n");
for(int i=0; i<len; i++){
printf("%d %p \n", ptr[i], ptr+i);
}
printf("\n");
}
int input(int *ptr, int *size){
int x;
printf("Insert your numbers:\n");
while(1){
scanf("%d", &x);
if(x == -1) break;
size[0]++;
// printf("Size: %d\n", size[0]);
ptr = realloc(ptr, size[0] * sizeof(int));
if(ptr == NULL) return -1;
int indexToStore = size[0]-1;
// printf("Index: %d\n", indexToStore);
ptr[indexToStore] = x;
output(ptr, size[0]);
}
return 0;
}
int main()
{
int size = 0;
int *array = (int*)malloc(sizeof(int));
if(array == NULL) return -1;
input(array, &size);
output(array, size);
input(array, &size);
output(array, size);
free(array); array = NULL;
return 0;
}
The argument ptr of the function input is a copy of what is passed and modifying that will not affect what is passed in caller.
Therefore, the change in buffer via realloc() is not saved from the 1st call of input() to the 2nd call of input() and it will lead to troubles.
You should pass a pointer to what should be modified to have functions modify caller's local things.
#include <stdio.h>
#include <stdlib.h>
void output(int *ptr, int len){
printf("\n");
for(int i=0; i<len; i++){
printf("%d %p \n", ptr[i], ptr+i);
}
printf("\n");
}
int input(int **ptr, int *size){
int x;
printf("Insert your numbers:\n");
while(1){
scanf("%d", &x);
if(x == -1) break;
size[0]++;
// printf("Size: %d\n", size[0]);
*ptr = realloc(*ptr, size[0] * sizeof(int));
if(*ptr == NULL) return -1;
int indexToStore = size[0]-1;
// printf("Index: %d\n", indexToStore);
(*ptr)[indexToStore] = x;
output(*ptr, size[0]);
}
return 0;
}
int main()
{
int size = 0;
int *array = (int*)malloc(sizeof(int));
if(array == NULL) return -1;
input(&array, &size);
output(array, size);
input(&array, &size);
output(array, size);
free(array); array = NULL;
return 0;
}
New picture from an external compiler.. the exit code is ok?
enter image description here
This is the full code. I'm having a trouble program blows away after printing the wanted output to the screen. I guess it's a problem with the way I allocated memory for the array of structs, and the .name field of each struct in a for loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_NAME_LEN 50
typedef struct stud
{
char *name;
int marks[4];
float avg;
}student;
student* Create_Class(int);
void Avg_Mark(student*);
void Print_One(student*);
void printExcellent(student*);
void main()
{
int size, i;
student *arr, *newArr;
printf("\nEnter the number of students: ");
scanf_s("%d", &size);
newArr = Create_Class(&size);
for (i = 0; i < size; i++)
{
printExcellent(newArr+i);
}
for (i=0;i<size;i++) free(newArr[i].name);
free(newArr);
_getch();
}
student* Create_Class(int size)
{
student *p;
char str[MAX_NAME_LEN];
int i, j;
p = (student*)calloc(size , sizeof(student));
if (!p)
{
printf("Memory allocation failure.");
exit(1);
}
for (i = 0; i < size; i++)
{
printf("Enter your name: ");
rewind(stdin);
gets(str);
p[i].name = (char*)calloc(strlen(str)+1,sizeof(char));
if (!(p[i].name))
{
printf("Memory allocation error!");
exit(1);
}
strcpy_s(p[i].name,50,str);
printf("Enter your marks: ");
for (j = 0; j < 4; j++)
{
scanf_s("%d", &p[i].marks[j]);
}
Avg_Mark(p + i);
}
return p;
}
void Avg_Mark(student* s)
{
int i, sum=0;
for (i = 0; i < 4; i++)
sum += s->marks[i];
s->avg = (float)sum / 4;
}
void Print_One(student* s)
{
printf("The average of %s is %.1f\n", s->name, s->avg);
}
void printExcellent(student* s)
{
if ((s->avg) > 85)
Print_One(s);
}
Gonna point out everything fishy I see for you:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX_NAME_LEN 50
typedef struct stud
{
char *name;
int marks[4];
float avg;
}student;
student* Create_Class(int);
void Avg_Mark(student*);
void Print_One(student*);
void printExcellent(student*);
void main()
{
int size, i;
student *arr, *newArr;
printf("\nEnter the number of students: ");
scanf_s("%d", &size);
// This is wrong. Remove the &...
newArr = Create_Class(&size);
for (i = 0; i < size; i++)
{
printExcellent(newArr+i);
}
for (i=0;i<size;i++) free(newArr[i].name);
free(newArr);
_getch();
}
student* Create_Class(int size)
{
student *p;
char str[MAX_NAME_LEN];
int i, j;
// Consider checking size for a sane value.
// Ok, allocate an array of students.
p = (student*)calloc(size , sizeof(student));
if (!p)
{
printf("Memory allocation failure.");
exit(1);
}
for (i = 0; i < size; i++)
{
printf("Enter your name: ");
// These 2 lines scare the heck out of me. I'd really do this differently.
// gets is the devil and the see:
// https://stackoverflow.com/questions/20052657/reversing-stdin-in-c
// for why this may not work well.
rewind(stdin);
gets(str);
// What if str is not a terminated string? Then 1 char of 0? Guess this is ok. Hope it doesn't overflow on the copy below though (consider fixed max size and not using a temporary)
p[i].name = (char*)calloc(strlen(str)+1,sizeof(char));
if (!(p[i].name))
{
printf("Memory allocation error!");
exit(1);
}
// Do a fast copy of up to 50 chars. I'd really want to verify this output to be sure it works.
strcpy_s(p[i].name,50,str);
printf("Enter your marks: ");
for (j = 0; j < 4; j++)
{
// Hope this inputs the way you want.
scanf_s("%d", &p[i].marks[j]);
}
// This should work, but I prefer more explicit pointers.
Avg_Mark(p + i);
}
return p;
}
void Avg_Mark(student* s)
{
// What if s is Null?
int i, sum=0;
// 4 is a magic number. Make this a constant.
for (i = 0; i < 4; i++)
sum += s->marks[i];
// This won't be as accurate as you want. Consider an integer solution.
s->avg = (float)sum / 4;
}
void Print_One(student* s)
{
// What if s is Null? What about s->name?
printf("The average of %s is %.1f\n", s->name, s->avg);
}
void printExcellent(student* s)
{
// What if s is Null?
if ((s->avg) > 85)
Print_One(s);
}
Note: While going through this code, I did not see any "red flags" except for the & on the size and perhaps the gets/rewind calls. I'd still add null asserts to your functions and also walk through it with a debugger to be sure that everything is as you expect. Honestly, there is enough going on here that I'd prefer the debugger help to my quick trace of the code while I was writing comments.
Update
If I change all your scanf_s to scanf() calls, replace your gets() / rewind() calls to a simple scanf("%s", str) call, and change your funky strcpy_s() function to a simpler strcpy() or strncpy() call, your program does not seem to crash for me. My money is that the strcpy_s() call is corrupting RAM while doing its "fast" copy.
I'm new to this fantastic site and I found it very useful in many occasions, but now I'm dealing with a problem which I can't seem to solve.
I'm a beginner C student and I'm studying dynamic memory allocation.
I want to create a dynamic array of, let's say, integers, allocating memory for it inside a procedure, not in the main function.
This works:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *numbers; // pointer to create a dynamic array
int *test; // pointer to test realloc function
int c;
size_t i = 0;
unsigned int dim; // array's length
// allocate memory for the 1st number
numbers = malloc(sizeof(*numbers));
if (numbers == NULL) {
fputs("Error while allocating memory!\n", stderr);
} else {
printf("Insert the 1 number: ");
scanf("%d", &numbers[0]);
++i;
/* allocate memory for the other numbers,
* until the user inputs EOF
*/
while (!feof(stdin)) {
test = realloc(numbers, (sizeof(*numbers) * (i + 1)));
if (test == NULL) {
fputs("Error while allocating memory!\n", stderr);
} else {
numbers = test;
printf("Insert the %u number: ", i + 1);
scanf("%d", &numbers[i]);
++i;
}
}
dim = --i;
// print the array, 5 numbers per line
for (i = 0; i < dim; ++i) {
if (i % 5 == 0) {
puts("");
}
printf("%d ", numbers[i]);
}
puts("");
}
getchar();
return 0;
}
But I want to do this (which doesn't work):
#include <stdio.h>
#include <stdlib.h>
void get_numbers(int **numbers, unsigned int *dim);
int main()
{
int *numbers; // pointer to create a dynamic array
size_t i = 0;
unsigned int dim; // array's length
get_numbers(&numbers, &dim);
// print the array, 5 numbers per line
for (i = 0; i < dim; ++i) {
if (i % 5 == 0) {
puts("");
}
printf("%d ", numbers[i]);
}
puts("");
getchar();
return 0;
}
void get_numbers(int **numbers, unsigned int *dim)
{
int *test; // pointer to test realloc function
int c;
size_t i = 0;
// allocate memory for the 1st number
*numbers = malloc(sizeof(*numbers));
if (*numbers == NULL) {
fputs("Error while allocating memory!\n", stderr);
} else {
printf("Insert the 1 number: ");
scanf("%d", &numbers[0]); // Maybe the error is here
++i;
/* allocate memory for the other numbers,
* until the user inputs EOF
*/
while (!feof(stdin)) {
test = realloc(*numbers, (sizeof(*numbers) * (i + 1)));
if (test == NULL) {
fputs("Error while allocating memory!\n", stderr);
} else {
*numbers = test;
printf("Insert the %u number: ", i + 1);
scanf("%d", &numbers[i]);
++i;
}
}
*dim = --i;
}
}
Do I need to use a pointer to a pointer to int, right?
I know I made some mistakes but I can't figure out how to fix them.
Thanks in advance for your replies!
Bye,
Fabio Nardelli
In the first block of code, numbers is of type int*.
In the second block of code, numbers is of type int**.
You have not fixed all the places that need to be fixed because of that change.
My suggestion:
Change the input argument to numbersPtr.
Create a local variable int* numbers.
Before you return from the function, set *numbersPtr to number.
void get_numbers(int **numbersPtr, unsigned int *dim)
{
int* numbers;
...
*numbersPtr = numbers;
}
It will be better to change get_numbers to return numbers.
int* get_numbers(unsigned int *dim)
{
int* numbers;
...
return numbers;
}
and change its usage as:
numbers = get_numbers(&dim);
I am a new C programming student and am having trouble with a code I am currently working on. I need to ask the user for a 12 number barcode with spaces in between each value. Also, I will need to refer to each individual value in the array later on in the code. For example, if my array is x[12], I need to use x[1], x[2], and all other values to calculate the odd sum, even sum, etc. Below is my first function to read in the bar code using a for loop. Any assistance for the script of this function would help.
#include <stdio.h>
#define ARRAY_SIZE 12
int fill_array() {
int x[ARRAY_SIZE], i;
printf("Enter a bar code to check. Separate digits with a space >\n");
for(i=0; i<ARRAY_SIZE; i++){
scanf("% d", &x);
}
return x;
}
You should pass array to read as the argument and store what is read there.
Also note that % d is a invalid format specifier for scanf().
#include <stdio.h>
#define ARRAY_SIZE 12
/* return 1 if suceeded, 0 if failed */
int fill_array(int* x) {
int i;
printf("Enter a bar code to check. Separate digits with a space >\n");
for(i=0; i<ARRAY_SIZE; i++){
if(scanf("%d", &x[i]) != 1) return 0;
}
return 1;
}
int main(void) {
int bar_code[ARRAY_SIZE];
int i;
if(fill_array(bar_code)) {
for(i=0; i<ARRAY_SIZE; i++) printf("%d,", bar_code[i]);
putchar('\n');
} else {
puts("failed to read");
}
return 0;
}
Alternately, you can allocate an array in the function and return its address.
#include <stdio.h>
#include <stdlib.h>
#define ARRAY_SIZE 12
/* return address of array if succeeded, NULL if failed */
int* fill_array(void) {
int *x, i;
x = malloc(sizeof(int) * ARRAY_SIZE);
if (x == NULL) {
perror("malloc");
return NULL;
}
printf("Enter a bar code to check. Separate digits with a space >\n");
for(i=0; i<ARRAY_SIZE; i++){
if(scanf("%d", &x[i]) != 1) {
free(x);
return NULL;
}
}
return x;
}
int main(void) {
int *bar_code;
int i;
if((bar_code = fill_array()) != NULL) {
for(i=0; i<ARRAY_SIZE; i++) printf("%d,", bar_code[i]);
putchar('\n');
free(bar_code);
} else {
puts("failed to read");
}
return 0;
}
I tried to write a program which used a function to read the data, store in an array and then tried to return the array to main() for the sorting process.
Here's what I have
#include <stdio.h>
#include <stdlib.h>
#define RESERVED_ARRAY_SIZE 100
int* read_data_from_files()
{
int* arr = (int*) malloc(sizeof(int) * 50);
int arr_count;
int status;
int i,j ;
arr_count =0;
i=0;
FILE *cfPtr;
if ((cfPtr = fopen ("score.dat", "r"))== NULL)
{
printf("File cannot be opend\n");
}
else
{
fscanf(cfPtr, "%d", &arr[i]);
printf("%5d%5d\n", i, arr[i]);
while (!feof (cfPtr))
{
++i;
++arr_count;
status = fscanf(cfPtr, "%d", &arr[i]);
if (status == EOF)
{
break;
}
else if (status != 1)
{
fprintf (stderr, "Error input\n");
getchar();
}
if (arr_count >= RESERVED_ARRAY_SIZE)
{
fprintf(stderr, " The number of array is over.\n");
getchar();
}
printf("%5d%5d\n", i, arr[i]);
}
fclose (cfPtr);
}
printf("arr_count=%d\n", arr_count);
return arr;
}
int main()
{
int i;
int arr_count;
int* arr = (int*) malloc(sizeof(int) * 10);
arr = read_data_from_files();
arr_count = sizeof(arr)/sizeof(arr[0]);
printf("this check the size of array%d", arr_count);
for (i=0; i<9; i++)
{
printf("%d ", i);
printf("%d", *arr[i]);
}
selection_sort(arr, arr_count);
getchar();
return 0;
}
My questions are
1. If functions cannot return 2 values, what shuold I do to pass the array size(the arr_count in my program) in the funcution to the main()?
2. If the function only returns the pointer of the array to the main(), can I figure out the size of the array in the main? what should I do ?
Thans a lot for your assistants.
No, you cannot figure out the size automatically. The standard way to return multiple values is to pass a pointer to a variable in which you want to store the extra return value:
int* read_data_from_files(int *result_size)
{
...
*result_size = arr_count;
...
return arr;
}
int main()
{
int arr_count;
int *arr = read_data_from_files(&arr_count);
...
}
You could make a variable size in the main function, and have the function call pass the address to read_data_from_files() as an additonal argument. read_data_from_files() can then modify the value from inside the function, and you won't need to return it.