I have read a lot of questions of stackoverflow, but couldn't find any solutions on how to deal with this problem of allocating and manipulating pointers inside functions: Can anybody please tell me what's wrong with this code? (I want to allocate and assign values to *D through pointerpass and print the same through pointerprint)
#include<stdio.h>
#include<stdlib.h>
float *D;
void pointerpass(float **ptr1)
{
*ptr1=(float*)malloc(3*sizeof(float));
*(ptr1+0)=1.33;
*(ptr1+1)=2.33;
*(ptr1+2)=3.33;
}
void pointerprint(float **ptr2)
{
int j=0;
for (j=0;j<3;j++)
printf("\n%f\n",*(ptr2+j));
}
int main()
{
pointerpass(&D);
pointerprint(&D);
return 0;
}
Here we go
#include<stdio.h>
#include<stdlib.h>
float * pointerpass(){
float *ret = malloc(3*sizeof(float));
ret[0] = 1.33f;
ret[1] = 2.33f;
ret[2] = 3.33f;
return ret;
}
void pointerprint(float *array) {
int j=0;
for (j=0;j<3;j++) {
printf("\n%f\n",array[j]);
}
}
int main() {
float *x = pointerpass();
pointerprint(x);
free(x); // We do not like memory leaks
return 0;
}
void pointerpass(float **ptr1){
*ptr1=(float*)malloc(3*sizeof(float));
(*ptr1)[0]=1.33;
(*ptr1)[1]=2.33;
(*ptr1)[2]=3.33;
}
void pointerprint(float **ptr2){
int j=0;
for (j=0;j<3;j++)
printf("\n%f\n", (*ptr2)[j]);
}
Related
I have a problem, because while loading data from the console, a bug is popping up I think it's a tricky thing to pass the indicator through the function, but I don't know how to fix it.
#include<stdio.h>
#include<stdlib.h>
typedef struct rn{
int n; /**numerator**/
unsigned d; /**denomirator**/
} rationalNumber;
typedef struct dot{
rationalNumber x;
rationalNumber y;
} point;
int gcd(int a, int b)
{
if(b!=0)
return gcd(b,a%b);
return a;
}
void input(rationalNumber *a)
{
int nwd;
if (scanf("%d/%u",&(a->n), &(a->d)) == 1) a->d=1;
else
{
nwd = abs(gcd(a->n, a->d));
a->n = a->n/nwd;
a->d = a->d/nwd;
}
}
void load_point(point *a, void(*function)(rationalNumber *))
{
function(&a->x);
function(&a->y);
}
int main(void)
{
rationalNumber *z;
point *a;
load_point(a, input);
return 0;
}
I've got this message : Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
in this place : if (scanf("%d/%u",&(a->n), &(a->d)) == 1) a->d=1;
You're creating pointers that point to nothing in particular and then pass these on to the function which never initializes them, allocates memory for them, and trusts that they're valid, but they're not.
Remember that point* a is a pointer, not an allocation.
An easy solution is to use local variables instead of pointers:
int main(void)
{
rationalNumber z;
point a;
load_point(&a, input);
return 0;
}
#include<stdio.h>
#include<conio.h>
int step_counter(char *array);
int main()
{
char *txt = "Try...";
printf("%d",step_counter(&txt));
getch();
}
int step_counter(char *array)
{
int step=0;
while(*array==NULL)
{
array++;
step++;
}
array-=step;
return step;
}
I need to send a pointer to a function without array. How can I solve this problem? I'm tired because of trying to solve this problem for months...
May be this is what you're trying to achieve.
#include<stdio.h>
int step_counter(char *array);
int main()
{
char *txt = "Try...";
printf("%d",step_counter(txt));
return 0;
}
int step_counter(char *array)
{
int step=0;
while(*array)
{
array++;
step++;
}
return step;
}
Edited
First, txt is a pointer to character array, so you don't have to send &txt to pass its address because txt itself is an address. And second, in the while loop you can either use while(*array) or while(*array != '\0') to check character array termination. And oh! as alk pointed out, array-=step; is redundant.
I am trying to dynamically allocate a contiguous block of memory, store some integer value and display it.
#include<stdio.h>
#include<conio.h>
void main()
{ int i;
int *ptr;
ptr=(void *)malloc(sizeof(int)*5); //allocation of memory
for(i=0;i<5;i++)
{ scanf("%d",&ptr[i]);
}
for(i=0;i<5;i++)
{ printf("%d",*ptr[i]); //error is found here``
}
} }
ptr[i] means the value at address (ptr+i) so *ptr[i] is meaningless.You should remove the *
Your corrected code should be :
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
int i;
int *ptr;
ptr=malloc(sizeof(int)*5); //allocation of memory
for(i=0;i<5;i++)
{ scanf("%d",&ptr[i]);
}
for(i=0;i<5;i++)
{ printf("%d",ptr[i]); //loose the *
}
return 0;
} //loose extra }
so i get this conversion problem char to const char. how can i get this code to work?
please, don't use pointers because im a newbie and i dont know anything about them... Thanks :)
#include<stdio.h>
#include<string.h>
char a[50];
int power(char a) {
int b;
b=0;
if(strlen(a)-b==0)
return 0;
else if(a[b]=='x'){
return power(a,b+1)+1;
}
else{
return power(a,b+1);
}
}
}
int main()
{
scanf("%s",&a);
printf("%d",power(a));
return 0;
}
power() should take an array (or a pointer, but you asked to keep those out, so...)
And b needs to be a parameter, not a variable that's always 0.
int power(char a[], int b) {
if(strlen(a)-b==0)
return 0;
else if(a[b]=='x'){
return power(a,b+1)+1;
}
else{
return power(a,b+1);
}
}
Finally, your scanf() call doesn't need a pointer to the address of the array:
int main()
{
scanf("%s",a);
printf("%d",power(a, 0));
return 0;
}
This is giving me a segfault at the memset and I have no idea why, I am going to a specific index of a 2D array, this should give me a char pointer and allow me to use memeset.
void test(char** test)
{
int i;
for(i=0;i<20;i++)
{
memset(test[i],0,sizeof(char)*1);
return;
}
}
int main()
{
char thing[20][20];
int i;
for(i=0;i<20;i++)
{
memset(thing[i],0,sizeof(char)*20);
}
test(thing);
return 0;
}
Your parameter declaration is incorrect, it should be:
void test(char test[20][20])
or:
void test(char test[][20])