can you tell me why this doesn't work.
int function(int);
int main()
{
int g[20],N;
printf("Type N");
scanf("%d",&N);
g[20]=function(N);
printf("s[0] is %d\n",g[0]);
printf("s[1] is %d\n",g[1]);
printf("s[2] is %d\n",g[2]);
}
int function(int N){
int s[20];
s[0]=1;
s[1]=3;
s[2]=5;
return s[20];
}
I just want that my function return this numbers 1,3,5 but it returns some weird numbers, i thinks it's adresses or something.
PS. I just began to learn C.
You need to something like this.
void function(int *);
int main()
{
int g[20],N; // array g local to main funciton
printf("Type N");
scanf("%d",&N);
function(g); // invoke function by passing array base address as an argument
printf("s[0] is %d\n",g[0]); // the first three positions of the g array
printf("s[1] is %d\n",g[1]); // have not been set by function
printf("s[2] is %d\n",g[2]); // they are also all unknown values
}
void function(int *s){
s[0]=1; //*(s+0)
s[1]=3;
s[2]=5;
}
From your code, you seem to have some misunderstanding about arrays and scope. The two arrays you declare start with unknown values until you set them. see my comments to your original code:
int function(int);
int main()
{
int g[20],N; // array g local to main funciton
printf("Type N");
scanf("%d",&N);
g[20]=function(N); // invoke function with int typed in and return int
printf("s[0] is %d\n",g[0]) // the first three positions of the g array
printf("s[1] is %d\n",g[1]); // have not been set by function
printf("s[2] is %d\n",g[2]); // they are also all unknown values
}
int function(int N){ // you never use N in this function, why is it a parameter
int s[20]; // declare a int array local to function
s[0]=1; // you only set the first three items in array, rest are unknown values.
s[1]=3;
s[2]=5;
return s[20]; // return the 20th int item of the array, (unknown memory contents)
}
Related
I understand how to pass a 2D array to a function in C, but I would like to have the function update the original structure rather than a copy of it. How would I go about this? Why does the method I use create a copy of the structure - I was under the impression it was another syntax equivalent to using a pointer?
Thanks for any help. I've included code snippets underneath:
Declaring the variables
int R[rowsize][colsize], G[rowsize][colsize], B[rowsize][colsize];
int Rnew[rowsize][colsize], Gnew[rowsize][colsize], Bnew[rowsize][colsize];
Initialising the function
void blur(int rowsize, int colsize, int R[][428], int G[][428], int B[][428], int Rnew[][428], int Gnew[][428], int Bnew[][428]){
. . .
}
NB: Within this function, Rnew, Gnew and Bnew should be updated - each are a 2D array. I would like this to be done without returning anything.
Calling the function
blur(rowsize, colsize, R, G, B, Rnew, Gnew, Bnew)
You may try this way..
# include <stdio.h>
#define rowsize 3
#define colsize 3
int main()
{
int R[rowsize][colsize]={1,2,3,4,5,6,7,8,9};
int Rnew[rowsize][colsize];
copy_from_R_to_Rnew(R,&Rnew);
int i,j;
for(i=0;i<rowsize;i++)
{
for(j=0;j<colsize;j++){
printf("\t %d",Rnew[i][j]);
}
printf("\n");
}
return(0);
}
void copy_from_R_to_Rnew(int *R,int *Rnew)
{
int i,j;
for(i=0;i<rowsize;i++)
{
for(j=0;j<colsize;j++){
*(Rnew+i*colsize+j) = *(R+i*colsize+j);
}
}
}
I am new to C programming and I am trying to create functions. The first function executes but the second one doesn't.
#include <stdio.h>
char get_char();
int main(void)
{
char ch;
printf("Enter a character > ");
scanf("%c", &ch);
return ch;
}
int get_int()
{
int i;
printf("Enter an integer between 0 and 127 > ");
scanf("%d", &i);
return i;
}
For anyone else that arrives here, you may solve your problem, by making sure your main function is at the bottom of the file.
Why? Because if function a calls function b, a should be before b.
main is the entry point for your program. The C environment calls main when your program is executed. get_int() is not the name for an entry point, so the fact that you never call it directly or indirectly in main means it will never be executed.
You also didn't declare it before main meaning your compiler will warn about not finding it, but since get_int returns int it will link successfully regardless.
Fix:
int get_int();
int main ()
{
//...
}
int get_int()
{
//...
}
Your second function isn't called and it should be as follows:
int main()
{
int m = 10;
get_int(m);
return 0;
}
int get(int num)
{
int multiply = num * num;
return multiply;
}
I want to initialize a couple of arrays which are members of a struct which is passed to a function by reference.
Appreciate any help.
typedef struct Snake_pos
{
char field[10][10];
int Y[10];
int X[10];
}snake_pos;
int main()
{
snake_pos pos1;
pos_init(&pos1);
return 0;
}
void pos_init(snake_pos *pos1)
{
pos1->X={};
pos1->Y={};
pos1->field={};
}
You can only use the syntax = {} when defining the variable. So to zero every member you can either define it as
snake_pos pos1 = { 0 };
Or if it is passed to a function, like this
void pos_init(snake_pos *pos1)
{
memset(pos1, 0, sizeof *pos1);
}
You can access any array element inside of an array through a
structure pointer this way :
for 1 D array :
structure_pointer->array_valiable[index1]
for 2 D array :
structure_pointer->array_valiable[index1][index2]
so now, you can initialize each member of arrays X , Y and field of structure snake_pos using scanf() this way :
#include <stdio.h>
typedef struct Snake_pos
{
char field[10][10];
int Y[10];
int X[10];
}snake_pos;
void pos_init(snake_pos *pos1)
{
int i,j;
//initializing each member
//of array X
for(i=0;i<10;i++)
scanf("%d",&pos1->X[i]);
//of array Y
for(i=0;i<10;i++)
scanf("%d",&pos1->Y[i]);
//of the two dimensional array field
for(i=0;i<10;i++)
for(j=0;j<10;j++)
scanf(" %c",&pos1->field[i][j]);
//notice the space before %c is to consume white spaces returning from before scanf's
}
int main()
{
snake_pos pos1;
pos_init(&pos1);
return 0;
}
Note : if you want to initialize all members to a single fixed value,
then you can instead of using scanf() in for loop, assign the value
this way :
for(i=0;i<10;i++) //initializes all members with a value of 0
pos1->X[i]=0;
If you want to initialize with zero's, Why not memset the structure ?
void pos_init(snake_pos *pos1)
{
memset(pos1, 0, sizeof(snake_pos));
}
My question is in general how to use pointers in functions correctly.
if to be more specific I need to write a function the recives 3 values from a user and then retruns it to the main one for further actions.
This is the code I have written so far:
#include <stdio.h>
#include <conio.h>
int inputThree(int, int, int);
int sortTwo(int, int);
int sortThree(int, int);
int main()
{
int a=0, b=0, c=0;
printf("before: func %d \n", b);
inputThree(a,b,c);
printf("after func: %d%d%d \n",a,b,c);
getch();
}
int inputThree(int a, int b, int c)
{
printf("Input three integers values: \n");
scanf("%d%d%d", &a, &b, &c);
return 0;
}
I'm intersted in understanding how to keep the values of scanf via pointers. When I return to the main function they are lost because they aren't global...
Also, I couldn't leave the function inputthree without parameters even though I want it to get them from scanf itself, so I had to put some values for it to run.
thanks in advance!
Pass pointers to the variables from main to inputThree.
Change the function declaration.
int inputThree(int* aPtr, int* bPtr, int* cPtr);
Change the call.
inputThree(&a, &b, &c);
Change the implementation.
int inputThree(int* aPtr, int* bPtr, int* cPtr)
{
printf("Input three integers values: \n");
scanf("%d%d%d", aPtr, bPtr, cPtr);
return 0;
}
You can either return a struct or make a function that handles passed pointers as argument.
#include <stdio.h>
struct Foo{
int x;
int y;
};
//one way
struct Foo do_work();
//or another
void do_work(int *x, int *y);
int main(void) {
return 0;
}
struct Foo do_work(){
//e.g.
struct Foo foo;
foo.x = 1;
foo.y = 2;
return foo;
}
void do_work1(int *x, int *y){
//e.g
*x = 1;
*y = 1;
}
Technically, only 1 thing (or none) can be returned from a function at a time. If you wanted to change the values of two or more variables via a function even after the function ends, you would need to pass into the function's parameters/arguments the memory reference of the variable.
Basically, why does it not just print the integers that are entered. Right now it just prints garbage value, but I do not know why it cannot access the values stored after it leaves the function. It only seems to get messed up after leaving the getIntegersFromUser function. If I run the for loop in the getIntegers function it does it properly, but why not in the main function?
Thanks in advance for your help.
#include <stdio.h>
#include <stdlib.h>
void getIntegersFromUser(int N, int *userAnswers)
{
int i;
userAnswers =(int *)malloc(N*sizeof(int));
if (userAnswers)
{ printf("Please enter %d integers\n", N);
for (i=0;i<N; i++)
scanf("%d", (userAnswers+i));
}
}
int main()
{
int i, M=5;
int *p;
getIntegersFromUser(M, p);
for (i=0;i<5;i++)
printf ("%d\n", p[i]);
return 0;
}
Also, this is a homework question, but it's a "Bonus Question", so I'm not trying to "cheat" I just want to make sure I understand all the course material, but if you could still try to give a fairly thorough explanation so that I can actually learn the stuff that would be awesome.
Pointers are passed by value. The function is using a copy of your pointer, which is discarded when the function ends. The caller never sees this copy.
To fix it, you could return the pointer.
int *getIntegersFromUser(int N)
{
int *userAnswers = malloc(...);
...
return userAnswers;
}
/* caller: */
int *p = getIntegersFromUser(M);
Or you could pass your pointer by reference so the function is acting on the same pointer, not a copy.
void getIntegersFromUser(int N, int **userAnswers)
{
*userAnswers = (int *) malloc(N*sizeof(int));
...
}
/* caller: */
int *p;
getIntegersFromUser(N, &p);