I am new to working with pointers and I have no idea why this does not work. If I print inside the scan function it works, but it has to be in a separate function.
#include <stdio.h>
#include <stdlib.h>
int scan(int n, int *buffer)
{
int i;
printf("nr of elements:\n");
scanf("%d", &n);
//int *buffer, i=0;
// *buffer=(int*)malloc((*n) * sizeof(int));
buffer = malloc((n + 1) * sizeof(*buffer));
for(i=0; i<n; i++){
buffer[i]=i+1;
printf ("%d ", buffer[i]);}
printf ("\n-------------\n");
}
void print(int *n,int *buffer){
int i;
for(i=0; i<n; i++){
printf ("%d ", buffer[i]);}
}
int main()
{
int n,i, *buffer;
scan(&n, &buffer);
print(n, *buffer);
printf("i quit\n");
for(i=0; i<n; i++){
printf ("%d ", buffer[i]);
}
return 0;
}
It can help to use (slightly) more descriptive variable names to make it abundantly clear that 'n' in scan is not the same as 'n' in print:
#include <stdio.h>
#include <stdlib.h>
void scan(int *np, int **bufferp)
{
int i;
printf("nr of elements:\n");
scanf("%d", np);
*bufferp = (int*)malloc(*np * sizeof(**bufferp));
for(i=0; i < *np; i++){
(*bufferp)[i] = i + 1;
printf ("%d ", (*bufferp)[i]);
}
printf ("\n-------------\n");
}
void print(int n, int *buffer){
int i;
for(i=0; i<n; i++){
printf ("%d ", buffer[i]);
}
printf("\n");
}
int main()
{
int n, i, *buffer;
/* pass references to n and buffer for scan to modify */
scan(&n, &buffer);
print(n, buffer);
return 0;
}
There are two ways of changing parameters of function (change data outside function):
1) use references (in my example - the first argument of scan function)
2) use pointers (in my example - the second argument of scan... THIS ARGUMENT IS OF TYPE int*)
Try this (my comments show the changes):
#include <stdio.h>
#include <stdlib.h>
void scan(int &n, int ** buffer) // pointer to pointer for bufer, void instead of int for function, and &int for n
{
int i;
printf("nr of elements:\n");
scanf("%d", &n);
*buffer = (int*) malloc( n * sizeof(**buffer)); // (int*) should be, and *buffer
for(i=0; i<n; i++){
(*buffer)[i]=i+1; // *buffer
printf ("%d ", (*buffer)[i]); // *buffer
}
printf ("\n-------------\n");
}
void print(int n, int *buffer) // type of n changed from int* to int
{
int i;
for(i=0; i<n; i++){
printf ("%d ", buffer[i]);
}
}
int main()
{
int n,i, *buffer = NULL;
scan(n, &buffer);
print(n, buffer); // buffer is pointer, no need to use *
printf("i quit\n");
for(i=0; i<n; i++){
printf ("%d ", buffer[i]);
}
return 0;
}
Note: if you whant to change data you can use
void changeArgument(int * data)
{
(*data)++;
}
but to change addres of data, you should use
void changeArgument(int ** pointer)
{
(*pointer) = (int*)malloc(sizeof(int));
}
Either declare the scan() with void return type and display the data their itself.
OR if you want the function to return int then instead of printing it in the function return the value and display it in main(). It should work fine then.
Related
I wrote a program to get an array which will return an array with elements as difference between max value and remaining elements.
#include <stdio.h>
void behind(int *, int);
int main(void) {
int array[10];
int N, i;
scanf("%d", &N);
for (i=0; i<N; i++) {
scanf("%d", &array[i]);
}
behind(array, N);
for (i=0; i<N; i++) {
printf("%d\n", array[i]);
}
return 0;
}
void behind(int *ptr,int size) /* Write your function behind() here: */
{
int i,max=0;
for(i=1;i<size;i++){
if(ptr[i-1]>=ptr[i])
max=ptr[i-1];
else
max = ptr[i];
}
for(i=0;i<size;i++);
ptr[i]=max-ptr[i];
}
why my program always return the same array that I got as input?
I can't figure out how to print array elements from my function into the main program so if some can examine this code and help me fix it I would appreciate it. The program is supposed to take the length of the array from user input and then ask for its elements and print them out afterward.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int arrayN(int N) {
printf("Input array lenght: ");
scanf("%d",&N);
if(N>2) {
return N;
} else {
return 0;
}
}
int arrayelements(int array[], int array_length) {
int loop, i, N;
array_length = arrayN(N);
printf("Enter elements of the array: \n");
for(int i = 0; i < array_length; ++i) {
scanf("%d", &array[i]);
}
for(loop = 0; loop < array_length; loop++) {
printf("%d ", array[loop]);
}
}
int main() {
int N, array[], array_length;
int b = arrayelements(array[], array_length);
int a = arrayN(N);
printf("Array length is: %d \n", a);
printf("Elements of array are: %d \n", b);
return 0;
}
I reworked your example code. Hope it is what you want.
Focus lied on fixing the array declaration issues, memory allocation and
user input.
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
int userinput_integer(const char *fmt, ...){
int N, rv = 0;
va_list va;
va_start(va, fmt);
vprintf(fmt, va);
while(1){
rv = scanf("%d", &N);
if (1 == rv) break;
printf("Input error! The input >>");
do{
rv = fgetc(stdin);
if (isprint(rv)) putchar(rv);
}while(rv != EOF && rv != '\n');
printf("<< is not a valid integer.\nPlease try again: ");
}
va_end(va);
return N;
}
int userinput_arraylength(void) {
int N;
N = userinput_integer("Input array lenght: ");
if(N>2) {
return N;
} else {
printf("Invalid length\n");
return 0;
}
}
int userinput_arrayelements(int *array, int N) {
printf("Enter elements of the array: \n");
for(int i = 0; i < N; ++i) {
array[i] = userinput_integer("%d: ", i);
}
return N;
}
void print_arrayelements(int *array, int N){
for(int i = 0; i < N; ++i) {
printf("%d ", array[i]);
}
}
int main() {
int N, *array;
N = userinput_arraylength();
array = malloc(N * sizeof(*array));
if (NULL == array){
printf("Allocation error!\n");
exit(-1);
}
N = userinput_arrayelements(array, N);
printf("Array length is: %d \n", N);
printf("Elements of array are:\n");
print_arrayelements(array, N);
free(array);
return 0;
}
Fistly, declaration of array is not correct.
It should be array[] = {0}
Secondly, you cannot call your array elements function before arrayN function, the size of array should be entered first
And in the array elements() there is no need to call the size function you can directly pass the size of array when calling the array elements ()
Here's the code:
#include <stdio.h>
#include<malloc.h>
int *getarray()
{
int size;
printf("Enter the size of the array : ");
scanf("%d",&size);
int *p= malloc(sizeof(size));
printf("\nEnter the elements in an array");
for(int i=0;i<size;i++)
{
scanf("%d",&p[i]);
}
return p;
}
int main()
{
int *ptr;
ptr=getarray();
int length=sizeof(*ptr);
printf("Elements that you have entered are : ");
for(int i=0;ptr[i]!='\0';i++)
{
printf("%d ", ptr[i]);
}
return 0;
}
I am taking a number in the main function, make it an array in make_array function. In the palindrome function, I need to check the array which i made in the make_array function but it is not visible in the palindrome function.
How can I solve this problem?
#include<stdio.h>
#define N 5
void make_array(int n);
int palindrome(int ar[],int size);
int main()
{
int num;
printf("Enter a number to check: ");scanf("%d",&num);
make_array(num);
if(palindrome(/*Don't know what should I write here*/))
printf("It is palindrome");
else
printf("It is not palindrome");
}
void make_array(int n)
{
int arr[N];
int digit,i=0;
while(n>0){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(i=0; i<N; i++)
printf("%d ",arr[i]);
}
int palindrome(int ar[],int size)
{
int i,j;
int temp[N];
j=N;
for(i=0; i<N; i++)
temp[i]=ar[i];
for(i=0; i<N; i++){
if(temp[j-1]!=ar[i])
return 0;
j--;
}
return 1;
}
The best way is to leave allocation to the caller. Then you can simply do
int main()
{
int num;
printf("Enter a number to check: ");scanf("%d",&num);
int array[num];
fill_array(num, array);
where fill_array does what "make_array" does in your code, minus the allocation part.
void fill_array(int num, int array[num]);
The palindrome function could be rewritten similarly:
int palindrome(int size, int array[size])
All of this uses the concept of variable-length arrays (VLA).
I have done some modification in your code so please refer it.
#include<stdio.h>
#include "stdafx.h"
#define N 5
int make_array(int n, int *arr);
int palindrome(int ar[],int size);
int main()
{
int num;
int arr[N];
int iRet;
printf("Enter a number to check: ");scanf_s("%d",&num);
iRet = make_array(num, arr);
if(palindrome(arr, iRet))
printf("It is palindrome");
else
printf("It is not palindrome");
}
int make_array(int n, int *arr)
{
//int arr[N];
int digit,i=0;
while(n>0){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(int j=0; j<i; j++)
printf("%d ",arr[j]);
return i;
}
int palindrome(int ar[],int size)
{
int i,j;
int temp[N];
j=size;
for(i=0; i<size; i++)
temp[i]=ar[i];
for(i=0; i<size; i++){
if(temp[j-1]!=ar[i])
return 0;
j--;
}
return 1;
}
The problem is with your make array function. When a function is called, the stack grows down and registers and pointers are allocated to save the point from which that function was called, now, here you send n by value and your function creates a place on the stack for an array that you fill- BUT- when your function returns- the stack pointer returns back up to the caller( if your function has a return value it will be saved in a pre-allocated place, but other than that all of the other function data on stack is unavailable.).
So in general, if you want a function to create an array that could be used later on it must be allocated on heap you can either return int* or send foo(int**) to the function that will hold the add. of the new allocated array.
another option is to allocate that array[N] in your main, and send foo(int arr[], int n, size_t size) to the function. Since the array was allocated by the caller in main- this memory will be valid for all of the main function life.
so option 1)
int main()
{
int num;
int* array;
printf("Enter a number to check: ");scanf("%d",&num);
array = make_array(num, N);
if(palindrome(array, N))
printf("It is palindrome");
else
printf("It is not palindrome");
free(array); /*free heap allocation */
}
int* make_array(int n, size_t size)
{
int* arr;
int digit ,i=0;
arr = malloc(sizeof(int)*size);
if(NULL == arr)
{
return NULL; /* malloc failed*/
}
while(n>0 && i<size){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(i=0; i<N; i++)
printf("%d ",arr[i]);
return arr;
}
or 2)
int main()
{
int num;
int array[N];/*array saved on stack in main function */
printf("Enter a number to check: ");scanf("%d",&num);
make_array(array,num, N);
if(palindrome(/*Don't know what should I write here*/))
printf("It is palindrome");
else
printf("It is not palindrome");
}
void make_array(int* arr, int n, size_t size)
{
int digit,i=0;
if(NULL == arr)/*if arr is not a valid pointer*/
{
return;
}
while(n>0 && i<size){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(i=0; i<N; i++)
printf("%d ",arr[i]);
}
I have two 2d arrays "a", "b" (empty array) with the same size, I have to change "a" due to a certain function that save it's new values in "b", then I have to change the new values according to the same function, so the program will save b's new values in a and then back to a.
When the arrays are printed only the first two ones are printed!!
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MSIZE 10
void new_gen(char * a[MSIZE],int s,char** b); /*the function we talked about*/
void print_m(char** b,int s); /*prints matrix*/
void cpy_m(char** b, char** a, int s);
int main()
{
int Size, gen, i, j;
printf("Enter number of generations\t");
scanf("%d", &gen);
printf("\nEnter size of the matrix (max size is %d and min is 2)\t", MSIZE);
scanf("%d", &Size);
char **m = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
m[i] = (char*) malloc(Size*sizeof(char));
}
printf("Enter matrix of first generation\n");
for (i=0; i<Size; i++) {
for (j=0; j<Size; j++) {
scanf(" %c", &m[i][j]);
}
}
print_m(m, Size);
for (i=1; i<gen; i++)
{
char **n = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
n[i] = (char*) malloc(Size*sizeof(char));
}
new_gen(m, Size, n);
print_m(n, Size);
cpy_m(n, m, Size);
}
return 0; }
void print_m(char** b, int s)
{
int i, j;
putchar('\n');
for (i=0; i<s; i++)
{
for (j=0; j<s; j++) {
printf("%c", *(*(b+i)+j));
}
putchar('\n');
}
return;
}
void cpy_m(char* b[MSIZE],char** a, int s)
{
int i, j;
for (i=0; i<s; i++) {
for (j=0; j<s; j++) {
*(*(a+i)+j) = b[i][j];
}
return;
}}
Consider this pair of nested loops
for (i=1; i<gen; i++)
{
char **n = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
n[i] = (char*) malloc(Size*sizeof(char));
}
new_gen(m, Size, n);
print_m(n, Size);
cpy_m(n, m, Size);
}
First point, both the loops use i as the control variable, but they are nested.
Second point, you overwrite the pointers from malloc in each iteration (there is no free).
I don't have a lot of experience with pointers, but I want to try to make an array of pointers, each pointer pointing to a scanned string.
For example, you first input how many strings you want to scan (for example 5), and then I want to scan those strings and make an array of 5 pointers that point to those strings.
Because I didn't have a lot experience with something like this, I first tried it with normal arrays instead of strings, what I got is this:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
int **array(int m, int n) {
int i, j;
int **array = malloc(n*sizeof(int*));
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
array[i]=malloc(m * sizeof(int));
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
int main(int argc, char*argv[]){
int m, n, *p, k;
scanf("%d %d", &m, &n);
printf("m is %d and n is %d\n", m, n);
p=*array(m, n);
printf("the numbers are:\n");
for (k=0; k<m*n; k++) {
printf("%d\n", p[k]);
}
return 0;
}
But here it's already going wrong, and I don't know why...
At the last printf, I always get wrong numbers, 0's and 17's...
Can someone explain me why this is and what I'm doing wrong? I think it's something with the returning of the array but I'm not sure..
If someone could explain this to me it would be great.
The problem with your code is the following:
// m = 3, n = 5
// array = ptr1, ptr2, ptr3, ptr4, ptr5
// | |
// 3 ints |
// 3 ints ..
int **array(int m, int n) {
int i, j;
int **array = (int**)malloc(n*sizeof(int*));
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
array[i]=(int*)malloc(m * sizeof(int));
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
In the above example (m=3, n=5) you allocated 5 pointers to integers and then tried to populate them by allocating memory at each iteration in the inner-loop (WRONG). If you allocate new memory at each iteration, you're gonna lose the pointer to the previously allocated memory and the data you stored!
Plus the indices seem to be wrong for the inner and outer loop, a correct version of your code is:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
// 3, 5
// array = ptr1, ptr2, ptr3, ptr4, ptr5
// | |
// 3 ints |
// 3 ints ..
int **array(int m, int n) {
int i, j, index;
int **array = (int**)malloc(n*sizeof(int*));
index = 0;
for (j=0; j<n; j++) {
array[j]=(int*)malloc(m * sizeof(int)); // Allocate once per each j
for (i=0; i<m; i++) {
array[j][i] = index++;
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
int main(int argc, char*argv[]){
int m, n, **p, k, i, j;
m = 3;
n = 5;
printf("m is %d and n is %d\n", m, n);
p=array(m, n);
printf("the numbers are:\n");
for (j=0; j<n; j++)
for(i=0; i<m; i++)
printf("%d\n", p[j][i]);
return 0;
}
And the above version is STILL NOT CORRECT : You need to free the allocated memory!
I'll leave that as an exercise.. hint: you CAN'T simply do "free(p);" :]
Are you sure about this for loop? If you've the malloc inside the inner loop you're not creating a matrix because every time you override the same cells...
int i, j;
int **array = malloc(n*sizeof(int*));
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
array[i]=malloc(m * sizeof(int));
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
It should be something like:
int i, j;
int **array = malloc(n*sizeof(int*));
for (i=0; i<n; i++) {
array[i]=malloc(m * sizeof(int));
for (j=0; j<m; j++) {
scanf("%d", &array[i][j]);
printf("array[%d][%d] is scanned and has value %d\n", i, j, array[i][j]);
}
}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<assert.h>
void *array(int m, int n) {
int i, j;
int (*array)[n] = malloc(sizeof(int [m][n]));//m*n*sizeof(int)
for (j=0; j<m; j++) {
for (i=0; i<n; i++) {
scanf("%d", &array[j][i]);
printf("array[%d][%d] is scanned and has value %d\n", j, i, array[j][i]);
}
}
return array;
}
int main(int argc, char*argv[]){
int m, n, *p, k;
scanf("%d %d", &m, &n);
printf("m is %d and n is %d\n", m, n);
p=(int*)array(m, n);
printf("the numbers are:\n");
for (k=0; k<m*n; k++) {
printf("%d\n", p[k]);
}
return 0;
}
#include <stdlib.h>
#include <stdio.h>
char **array(int m, int n) {
int i;
char **array = malloc(m*sizeof(char*));
for (i=0; i<m; ++i) {
array[i] = malloc(n*sizeof(char));//Fixed length : like char array[m][n] (char *array[m])
scanf("%s", array[i]);//!! There is no length constraints.
printf("array[%d] is scanned and has value %s\n", i, array[i]);
}
return array;
}
int main(int argc, char*argv[]){
int m, n, k;
char **p;
scanf("%d %d", &m, &n);
printf("m is %d and n is %d\n", m, n);
p=array(m, n);
printf("the string are:\n");
for (k=0; k<m; ++k) {
printf("%s\n", p[k]);
}
return 0;
}
I'm not sure if I do this smart, but I usually allocate the pointer array and then allocate the whole memory chunk to the first item. Then I get continuous memory for the data. Like:
_array = (float**) malloc( n * sizeof ( float * ));
assert ( _array != NULL );
_array[0] = (float*) malloc( n * m * sizeof ( float ));
assert ( _array[0] != NULL );
for ( idx = 0; idx < n; idx++ )
_array[ idx ] = _array[ 0 ] + idx * m;
(float instead of int in my case. And please don't comment on the return of malloc casting, nor on the silly user of assert())