I am getting error in this code as "invalid indirection" - c

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 }

Related

How to fix the error "comparison between pointer & integer"?

I have been trying to compare a structure variable and a string variable. But I am getting this error.
#include<stdio.h>
#include<string.h>
int main()
{
struct details {
char number[20];
} det[10];
int inp, i=0;
char aad;
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for(b=0;b<i;b++)
{
if(det[i].number==aad)
{
printf("Hello");
}
}
return 0;
}
Pls try to fix my error
after you look in the comments this will be the answer
#include<stdio.h>
#include<string.h>
#define SIZE 20 // add size for aad
int main()
{
struct details {
char number[20];
} det[10];
int inp, i = 0;
char aad[SIZE]; // must be array (can be with pointer, use malloc)
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for (b = 0; b < i; b++)
{
if (strcmp(det[b].number, aad) == 0) // strcmp to compare strings
{
printf("Hello");
}
}
return 0;
}

two functions (read and display an array) using pointers to structures

I just start to learn pointers to structures and I'm confused.I have to create a type of data ARRAY (which is associated with an array which contains integers.) like a structure which contains: numbers of array's elements and the array's elements stored in a part of memory(heap), dynamically allocated.
So I wrote:
typedef struct ARRAY
{
int nrElem; // number of elements
int *v[100];
};
Now I need to create 2 functions, one for reading an array from keyboard and the second one to display it using the structure I declared.
I tried but I get stuck.
void arrayDisplay(ARRAY *ps)
{
int i;
for(i=0;i<pd->nrElem;++i)
{
printf("%d",)
}
}
void readArray(ARRAY *ps)
{
int i;
for(i=0;i<pd->nrElem;++i)
{
printf("%d",)
scanf("%d",&);
}
}
How to continue?
Instead of an array of pointers int *v[100]; you need an array of ints int v[100]; in your data structure.
See code below:
#include <stdio.h>
#include <stdlib.h>
typedef struct ARRAY
{
int nrElem; // number of elements
int v[100];
} ARRAY;
void arrayDisplay(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d\n", ps->v[i]);
}
}
void readArray(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d: ", i);
scanf("%d",&ps->v[i]);
}
}
int main()
{
ARRAY a;
a.nrElem = 5;
readArray(&a);
arrayDisplay(&a);
return 0;
}
If you really want to use an array of int pointers you need to allocate the array first. And a different level of redirection for printf and scanf. But I'm not sure why you want to allocate memory for an integer array like this.
typedef struct ARRAY
{
int nrElem; // number of elements
int *v[100];
} ARRAY;
void arrayDisplay(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d\n", *ps->v[i]);
}
}
void readArray(ARRAY *ps)
{
int i;
for(i=0;i<ps->nrElem;++i)
{
printf("%d: ", i);
scanf("%d",ps->v[i]);
}
}
int main()
{
ARRAY a;
int i;
a.nrElem = 5;
for(i=0;i<a.nrElem;++i) {
a.v[i] = (int*)malloc(sizeof(a.v[i]));
}
readArray(&a);
arrayDisplay(&a);
return 0;
}

Allocating memory for an array of struct i get an error

the code is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
typedef struct word{
char word[20];
int occurrance;
} word;
int array_word_creator(word *array, FILE *fp);
void initialize(word array[], int max);
void comparator(word array[], int max, FILE *fp);
void printer(word array[], int max);
int main(int argc, char *argv[])
{
FILE *f_sent, *f_words;
word *array;
int arr_lenght=0;
if(argc!=3)
{
printf("Wrong argument number, please use NAME FILE1 FILE2;\n");
exit(EXIT_FAILURE);
}
if((f_sent=fopen(argv[1], "r"))==NULL||(f_words=fopen(argv[1], "r"))==NULL)
{
printf("Can't find or open the files, please check if the name is correct\n");
exit(EXIT_FAILURE);
}
arr_lenght=array_word_creator(array, f_words);
comparator(array, arr_lenght ,f_sent);
printer(array, arr_lenght);
return 0;
}
int array_word_creator(word *array, FILE *fp)
{
int n,i=0;
fscanf(fp,"%d",&n);
*array= malloc(n*sizeof(word));
while(fscanf(fp,"%s", array[i].word)!=EOF)
{
i++;
}
initialize(array,n);
return n;
}
void initialize(word array[], int max)
{
int i;
for(i=0;i<max;i++)
{
array[i].occurrance=0;
}
}
void comparator(word array[], int max, FILE *fp)
{
char word[MAX];
int i;
while(fscanf(fp,"%s", word)!=EOF)
{
for(i=0;i<max;i++)
{
if(strcmp(word, array[i].word)==0)
{
array[i].occurrance++;
}
}
}
}
void printer(word array[], int max)
{
int i;
for(i=0;i<max;i++)
{
if(array[i].occurrance>0)
{
printf("The word '%s' occurs %d times\n", array[i].word, array[i].occurrance);
}
}
}
And the compiler says me:
C:\Users\Matteo\Google Drive\Programming\C\lab3\es1\main.c|47|error: incompatible types when assigning to type 'word' from type 'void *'|
I just studied memory allocation so i'm having some trouble with it, especially with structures. If possible, plase link me also some good docs about this subject.
thank you!
In main word *array is a pointer to a structure of type word.
You then pass array, which does not point to anything, to the function array_word_creator.
You then try to assign the pointer returned by malloc to where array is pointing, but it doesn't point anywhere yet, and even if it did, it would be pointing to a word (since it is a word *), so it can't store a pointer, hence the compiler error.
If you want to set the array pointer in main to the result of malloc, you have to pass a pointer to the pointer. int array_word_creator(word **array, FILE *fp), then you would call it by doing array_word_creator(&array, .... ), the your *array = malloc will work.
You want this:
...
arr_lenght = array_word_creator(&array, f_words);
...
int array_word_creator(word **array, FILE *fp)
{
int n, i = 0;
fscanf(fp, "%d", &n);
*array = malloc(n * sizeof(word));
while (fscanf(fp, "%19s", (*array)[i].word) != EOF)
{
i++;
}
initialize(*array, n);
return n;
}

How to send a pointer to a function?(This is different)

#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.

Manipulating pointer values inside functions taking pointers as arguments

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]);
}

Resources