C | Pointer doesnt give the value - c

Im trying to do something easy, but i could find any solution.
I have an array, and I want to find an index, and update the pointer from a Function,
When Im trying to return the **Address of the index, and after it Print the Value at that index, I get nothing..
I hope you can Help me
int main()
{
int array[]={3,2,3,4};
int* pointer=NULL;
doIT((int *)array,4,pointer);
printf(" value of address %d",*pointer);
}
void doIT(int *p,int sizes,int** pointer){
for(int i =0;i<sizes;i++)
{
if(*p==4)
{
printf("TT %d",*p);
pointer=&p;
// printf("\n %d \n",pointer);
}
p++;
}
}

There are two errors: First, you have to call the function with
&pointer
and second, in the function body you have to set
*pointer=p;

Related

Recursive function is not working with a pointer

This function aims to return the number of zeroes in a number, num. The function rCountZeros2() passes the result through
the pointer parameter result.
`
void rCountZeros2(int num, int *result)
{
if (num==0)
return;
else
{
if (num%10==0){
(*result)++;
}
rCountZeros2(num/10, result);
}
}
`
See when you are invoking rCountZeros2() , my guess is value in variable result is not zero.It may be some garbage value or some other value from previous computation.However with details you have provided it is difficult to provide exact answer.
Kindly try the following standalone program, I got correct answer using your code
void rCountZeros2(int num, int *result)
{
if (num==0)
return;
else
{
if (num%10==0){
(*result)++;
}
rCountZeros2(num/10, result);
}
}
int main()
{
int result = 0;
int num=12300000;
rCountZeros2(num, &result);
printf("number of zeros in %d = %d",num ,result);
}

Filling an array with a string on a side function. Why does it prints gibberish?

I can't find help about this. Most issues i found are related to not closing the array with a '\0' , but this is not the case.
void main() {
char text[1000];
int index=0;
loadText(text,&index);
printf("\nThe text is:\n %s",text);
getch();
}
void loadText(char* text,int* index){
printf("Insert the text: \n");
while((*index<1000) && (text[*index]=getchar())!=EOF) {
*index++;
}
text[*index]='\0';
}
When i print the array it shows random chars.
On a side note, this is a test and we are forced to write the function as:
void loadText(char*, int*)
In
*index++;
doesn't increment the the value in the pointer. Instead it gets the value ie (*index) and then increment the pointer itself (index=index+1).
Do
(*index)++;

Sending values to another function using structures - Whats wrong with that?

I have two functions and a structure. My first function getChange does calculations and stores them in two arrays. My second function printChange should accept these values of two arrays and just print arrays using a simple for loop.
I am trying to use my structure Change to send these arrays across to the print function, but I can't seem to do it.
I thought of creating two arrays in my structure and then simply setting array in function = to array in struct and then do the opposite in print func. But I can't!
# include <stdio.h>
#include<stdlib.h>
struct cashback{
int value[8];
int money[8];
};
typedef struct cashback Change;
int getChange(int paid, int cost){
int r, k, cntr, c=1, value[8], money[8]={200,100,50,20,10,5,2,1};
Change store;
if (cost>paid){
printf("\t Insufficient Funds! \n");
getchar();
exit(1);
}
r=paid-cost;
value[0]=r/money[0];
k=r%money[0];
for(money[c], cntr=1;cntr<8;c++, cntr++){
if (k !=0){
value[c]= k/money[c];
k=k%money[c];
}
else{
value[c]=0;
k=k/money[c];
}
}
store.value[8]=value[8];
store.money[8]=money[8]; //Sending calculation to Struct(store)
}
void printChange() {
Change store;
int i, j;
for (i=0; i<8; i++) {
printf("%i \t",store.money[i] );
}
printf("\n \n");
for (j=0; j<8; j++) {
printf("%i \t ", store.value[j] );
}
}
int main(){
Change store;
getChange(90,50);
printChange();
getchar();
}
When you write:
Change store;
inside a function, that means there is a variable called store inside that function only. In your code you have three separate variables (all called store).
Instead you need to pass the variable around where it is needed.
You currently have getChange returning int but you never actually return a value. Instead you should make this return the change:
Change getChange(int paid, int cost)
{
Change change;
// ...
return change;
}
Similarly, the printChange function should be:
void printChange(Change change)
{
// ...
}
and your main function will look like:
Change store;
store = getChange(90, 50);
printChange(store);
OK, first of all inside the 'getChange', you don't use the 'Change' struct at all. Apart from using it in this declaration: Change a;
Also, there is certainly an error here: a.value =. With what is equal?
So, the money and value arrays that you declare inside 'getChange' have nothing to do with your struct.
And finally, it doesn't seem that printChange has anything to do with the rest of the program.
You can pass to both functions pointer to Change structure. The structure can be defined inside main:
int getChange(int paid, int cost, Change* a){
//...
a->value[c] = ...
//...
}
void printChange(Change* a) {
//...
printf("%i \t", a->value[i]);
//...
}
int main(){
Change a;
getChange(90,50, &a);
getchar(&a);
}

Initialize int array in struct in C

I have written a type:
typedef struct
{
int Tape[TAPE_SIZE];
int *Head;
int Tape_Count;
int Loop_Start_Count;
} Tarpit;
I try to initialize this type with the following function:
void Tarpit_Initialize(Tarpit Tarpit)
{
Tarpit.Tape_Count = 0;
Tarpit.Loop_Start_Count = 0;
int Index;
for(Index = 0; Index < TAPE_SIZE; Index++)
{
Tarpit.Tape[Index] = INITIAL_SIZE;
}
}
However, it does not seem to work. If I run this:
Tarpit Foo;
Tarpit_Initialize(Foo);
printf("Tarpit Initialization Test: \n");
int index;
for(index = 0; index < TAPE_SIZE ; index++)
{
if(Foo.Tape[index] == INITIAL_SIZE)
{
printf("%d: %d \n", index, Foo.Tape[index]);
}
else
{
printf("%d: %d !ERROR \n", index, Foo.Tape[index]);
}
}
I get several non-zero values (I have set #define TAPE_SIZE 10000 and #define INITIAL_SIZE 0)
Moreover, if I run the test without running Tarpit_Initialize(Foo), I get exactly the same results. The initializer does not seem to work. Why/how could I implement it in an other way? I would like to set every element of Foo.Tape to zero.
Problem solved!
You are passing Tarpit by value:
void Tape_Initialize(Tarpit Tarpit)
That means it is only a copy of Tarpit. You have to pass a pointer to it to be able to modify it.
void Tape_Initialize(Tarpit* Tarpit)
and pass it as pointer (note the name of the function called!):
Tape_Initialize(&Foo);
and the use the -> operator to modify it. For instance:
Tarpit->Tape_Count = 0;
Moreover, as "Elias Van Ootegem" pointed out, you should not use sizeof(Tarpit.Tape) to get the size of the array but TAPE_LENGTH that you defined. Because sizeof() will give you a size in bytes not in elements.
Have you checked the function u are calling ??
Its "Tarpit_Initialize(Foo);"
But the Function u are using it to initialize "void Tape_Initialize(Tarpit Tarpit)".
I think even what u have implemented should work fine .

Linked List Insert

I want to try insertion function for linked list. Function holds the record array and link array.for example;
head=0
string[0]="Angel" linked[0]=1
string[1]="Cesar" linked[1]=2
string[2]="Eduardo" linked[2]=3
string[3]="Pamela" linked[3]=-1/*-1 element does not show*/
head is Angel and Angel shows Eduardo.e.g. If you add element Denial,string[4]=Denial linked[4]=2 Denial shows Eduardo.Attention :---->>string[1]="Cesar" linked[1]=4 link array is updated.
I am getting some errors in this function.(element can not be added.) a little help
#define SIZE 10
int main()
{
int linked[SIZE]={3,0,4,-1,1};
char *str[]={"Ellian","Calanthe","Adela","Gardenia","Barbara",NULL};
return 0;
}
void list(int arr[],int head,int linky[]){
int adr=head;
while(adr!=-1){
puts(arr[adr]);
adr=linky[adr];
}
}
void insert(int arrr[],int head,int linky[],char element){
int k,N=0,prev,next;
for(k=0;arrr[k]<NULL;k++)
N++;
arrr[N]=element;
if(element>arrr[head])
{
prev=head;
next=linky[head];
while((next!=-1) && (arrr[next]<element)){
prev=next;
next=linky[next];
}
linky[prev]=N;
linky[N]=next;
}
else{
linky=head;
head=N;
}
N++;
listele(arr,N,linky);
}
replace
#define SIZE 10
by
int size = 10;
and replace
void insert(char str[],int link[],int size,int first)
by
void insert(char str[],int link[],int& size,int& first)
to start of. Still might not work but atleast now the things you do actually do what you want them to do
You are using your stringarray contents before initializing them, to avoid undefined behaviour change this:
char string[SIZE];
to this:
char string[SIZE] = {0};
If you want your string array to start with zeroes inside.
Also notice you are running past the last element of your arrays when you do:
link[size] = ...
as you array is of exactly size elements then the max index you can use is size - 1.

Resources