How to use free() function for a pointer array? [closed] - arrays

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've declared a struct and a pointer array by the declared struct.
struct Book* Collection[100];
Before the code ends I want to free up my memory using free function. free function is called up after printf() at printCollection()
void printCollection(struct Book* col, int num) {
int i;
for (i = 0; i < num; i++) {
printf("Name: %s Author: %s Page: %d Price: %d\n", col[i].name, col[i].author, col[i].page, col[i].price);
free(col[i]);
}
}
but I can't even build the program because of a error. Could I get some help?

You need to change printCollection to look like this:
void printCollection(struct Book* col[], int num) {
int i;
for (i = 0; i < num; i++) {
printf("Name: %s Author: %s Page: %d Price: %d\n",
col[i]->name, col[i]->author, col[i]->page, col[i]->price);
free(col[i]);
}
}
Your original function signature only passes one pointer to the function when you need to pass an array of pointers.

Related

getting runtime error SIGTSTP on codechef, can't find whats wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
getting runtime error SIGTSTP on codechef, can't find whats wrong, when i compile and this code on my system it work upto for loop after that it stuck.
this is my code
#include <stdio.h>
struct invent
{
char *name[20];
int number;
float price;
};
int main()
{
struct invent product[3], *ptr;
printf("input\n\n");
for(ptr = product; ptr < product+3; ptr++)
scanf("%s %d %f", ptr->name, &ptr->number, &ptr->price);
printf("\nOutput\n\n");
for(ptr = product;ptr < product + 3; ptr++)
{
printf("%-20s %5d %10.2f\n", ptr->name, ptr->number, ptr->price);
}
return(0);
}
You should init the struct before you filled it , and use a table of char instead of table of pointer of char
#include <stdio.h>
#include <string.h>
struct invent
{
char name[20];
int number;
float price;
};
int main()
{
struct invent product[3], *ptr;
printf("input\n\n");
for(ptr = product; ptr < product+3; ptr++) {
memset(ptr, 0 , sizeof(*ptr));
scanf("%s %d %f", ptr->name, &ptr->number, &ptr->price);
printf("\nOutput\n\n");
}
for(ptr = product;ptr < product + 3; ptr++)
{
printf("%20s %5d %10.2f\n", ptr->name, ptr->number, ptr->price);
}
return(0);
}

How to use if condition in structure in c [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to store name id in a structure. then I want to print them. Condition is if there is no store data in structure print "No data". But I can't do this in if condition.
#include<stdio.h>
struct store
{
char name[100];
int id[50];
} info[100];
int main()
{
int i=0;
printf("Enter your name\n");
scanf("%s",info[i].name);
printf("Enter your ID\n");
scanf("%d",info[i].id);
if(info.name[i]!='\0')
{
printf("%s",info[i].name);
}
else
{
printf("No data found");
}
}
When you refer to an array by its name only, it decays into a pointer to its 1st element. So info.name will not compile, it would need to be info->name instead. However, everywhere other than your if statement, you are using info[i].name to access the name of a specific element at index i, which is fine, but then you use info.name[i] in the if statement, which is not fine. See the difference? To access the 1st char of a name of a specific element, you would need info[i].name[0] instead.
Also, you are declaring the id field as an array of ints, but you really only need 1 int.
Also, you are not initializing the info array before filling it with data.
Try something more like this instead:
#include <stdio.h>
#include <string.h>
struct store
{
char name[100];
int id;
} info[100];
int main()
{
memset(info, 0, sizeof(info));
int i = 0;
printf("Enter your name\n");
scanf("%99s", info[i].name);
printf("Enter your ID\n");
scanf("%d", &(info[i].id));
if (info[i].name[0] != '\0')
{
printf("%d %s", info[i].id, info[i].name);
}
else
{
printf("No data found");
}
}

Refreshing pointer with integer and character [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i am not getting output for this program.
// Pointer and structure
I want get output as 1Jatin. but for refreshing pointer by using structure. i cant do that. It working as int and float. but not as int and char. any one please solved out this.
#include <stdio.h>
struct name{
int a;
char b;
};
int main(){
struct name *ptr ,p;
ptr = &p;
printf("Enter integer:");
scanf("%d", &(*ptr).a);
printf("Enter name:");
scanf("%s", &(*ptr).b);
printf("Displaying:");
printf("%d%s",(*ptr).a,(*ptr).b);
return 0;
}
#include <stdio.h>
#define MAX_NAME_LENGTH 32
struct name{
int a;
char b[MAX_NAME_LENGTH];
};
int main(){
struct name *ptr ,p;
ptr = &p;
printf("Enter integer:");
scanf("%d", &ptr->a);
printf("Enter name:");
scanf("%s", ptr->b);
printf("Displaying: ");
printf("%d %s\n",ptr->a,ptr->b);
return 0;
}
Many things:
You can simply us -> operator to dereference pointer to its members
C-Strings are null terminated arrays of chars. That means that b member must be an array large enough to store a name characters + null terminator ('\0', 0x00, 0).

How to pass arguments in __VA_ARGS to a 2d character array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to get result from __VA_ARGS to a function, and from there I need to pass string of each argument to a 2d character array.
If you are under C99, you can use compound literals
#include <stdio.h>
#define MACRO(...) func( \
sizeof((char *[]){__VA_ARGS__}) / sizeof(char *), \
(char *[]){__VA_ARGS__} \
)
void func(size_t n, char **p)
{
size_t i;
for (i = 0; i < n; i++) {
printf("%s\n", p[i]);
}
}
int main(void)
{
MACRO("abc", "def", "ghi");
return 0;
}
Notice that __VA_ARGS__ are evaluated twice in order to get the number of elements using sizeof, as an alternative you can send NULL as last parameter (sentinel):
#include <stdio.h>
#define macro(...) func((char *[]){__VA_ARGS__, NULL})
void func(char **p)
{
while (*p != NULL) {
printf("%s\n", *p);
p++;
}
}
int main(void)
{
macro("abc", "def", "ghi");
return 0;
}

new programmer here,whats wrong with this stack program? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
when i execute it the function do not work why?
#include<stdio.h>
struct stack{
int x[10];
int last;
};
void init(struct stack *s)
{
s->last=0;
}
void insert(struct stack *s)
{
int a;
while(a!=0)
{
int i;
printf("Enter the value\n");
scanf("%d",&i);
s->last++;
s->x[s->last]=i;
printf("%d",s->x[s->last]);
printf("enter 1 to continue 0 to exit\n");
scanf("%d",&a);
}
}
int main()
{
struct stack s;
int y,z;
printf("Trying out stacks\n");
printf("\n______________\n");
init(s);
insert(s);
return 0;
}
In function insert(), you declared
int a;
and then without initializing a you are doing the following,
while(a!=0)
will give Undefined Behaviour.
The following lines can leads buffer overflow,
s->last++;
s->x[s->last]=i; // no restriction applied on last
last can be more than 9 which can cause buffer overflow as x[10].

Resources