🚀 Loved the tutorial? Take it further with Programiz PRO! Refine your skills, conquer coding fears, and build confidence with interactive lessons, quizzes, and challenges. Strengthen your programming foundation and master C today! 👉Start Here: bit.ly/c-master
The best for beginners is to read a complete tutorial. Without skipping important steps. It's important to UNDERSTAND how things work, not just make them work
Great videos, Computer science can get really confusing at parts, especially when you go from java/python to c, currently taking a course in college, and these videos have helped me greatly
i have a test on tuesday about this subject and you helped me a lot on learning it. i really hope that more and more people will be helped by this and the other videos of yours. God bless y'all and thx for the great content :D
There's an error @ 8:15. "In our previous example we have allocated 400 bytes of memory to store 4 integer values." Each integer is four bytes, and thus you're allocating for four integers (4 bytes x 4 values) = 16 bytes of allocated memory.
Q. What is the correct way to allocate memory to store 12 double data? A. malloc(20 * double) B. (double*) malloc(12 * sizeof(double)) C. (double) malloc(20 * sizeof(double)) D. (double*) malloc( sizeof(20 * int))
I'm getting a warning for ptr = realloc(ptr, n * sizeof(int)); saying that realloc could return NULL, which "will cause the original memory block to be leaked", any know how to fix this?
[ Programiz Quiz ] Q. What is the correct way to allocate memory to store 12 double data? a. malloc(20 * double) b. (double*) malloc(12 * sizeof(double)) c. (double) malloc(20 * sizeof(double)) d. (double*) malloc( sizeof(20 * int))
The answer is b. The answer is choice b , because you are allocating a block of memory on the heap for 12 elements , each element being a double. 1: You allocating 12* sizeof(double) You can only use sizeof function on data types such as, sizeof(int), sizeof(float), sizeof(double) 2: and then you must type cast the void pointer to the block(array) of type double, so it can point to the right data type, in the above example, you type cast it to (double*).
@@errorhostnotfound1165 You need too! // Example program in C #include #include // needed for // malloc and free. #define ROWS 4 // number of // pointers to each array . #define COLS 2 // number of // elements in each array. int main(){ double ** matrix; // type is double in this example. // compiler needs this information // // each row(array) is a pointer to a // pointer to an array of size COLS. matrix= (double**)malloc(ROWS*sizeof(double*)); // Remember a multidimensional array is really an array which contains arrays. int j, k ; // needed for loops // next allocate each pointer in // each row to a pointer which points to an array with a size of COLS. // for(j=0; j< ROWS; j ++) matrix[i] = (double*)malloc(COLS*sizeof(double) ); // fill 2D array with values for(j= 0; j< ROWS; j ++) // inner loop for(k=0; k
When i run the realloc() code in visual studio code it returns an error as its expecting a void data type yet the malloc() worked. Can anyone 'point' me in the right direction? Thanks int *ptr, i , n1, n2; ptr = (int*) malloc(n1 * sizeof(int)); ptr = realloc(ptr, n2 * sizeof(int)); // error expecting void type in ptr ?
I finished the programming task and wanted to challenge myself by making it so the user decides how many spaces need to be added and what inputs should be added. I think I did it correctly but when I ran my code there was a "Segmentation Fault" which I don't know. Code: // Online C compiler to run C program online #include #include int main() { int n = 4; int* ages; int num1, num2, add; ages = (int*) malloc(n*sizeof(int)); if(ages == NULL){ printf("ERROR! Memory not allocated "); return 0; } printf("Enter your ages: "); for(int i = 0; i
#include #include int main() { int n = 4; int* age=(int*)malloc(n*sizeof(int)); if(n==0){ printf("MEMORY IS NOT AVAILABLE"); return 0; } printf("ENTER FOUR AGES: "); for(int i=0; i
// Online C compiler to run C program online #include #include int main() { int* ages; int n=4; ages=(int*) malloc(n* sizeof(int)); printf("Enter input values: "); for (int i=0; i
🚀 Loved the tutorial? Take it further with Programiz PRO!
Refine your skills, conquer coding fears, and build confidence with interactive lessons, quizzes, and challenges. Strengthen your programming foundation and master C today!
👉Start Here: bit.ly/c-master
"Dynamic memory allocation allows us to allocate memory dynamically" 10/10 explanation.
HAHAHAHHAHAHAHAH
The best for beginners is to read a complete tutorial. Without skipping important steps. It's important to UNDERSTAND how things work, not just make them work
actually some people understand better in practice
Your website has helped me a lot in my coding journey, thanks a lot for your contribution to the community.
Great videos, Computer science can get really confusing at parts, especially when you go from java/python to c, currently taking a course in college, and these videos have helped me greatly
i have a test on tuesday about this subject and you helped me a lot on learning it. i really hope that more and more people will be helped by this and the other videos of yours. God bless y'all and thx for the great content :D
good explanation; B for the question asked last.
You are doing an amazing job . I have learned alot
There's an error @ 8:15. "In our previous example we have allocated 400 bytes of memory to store 4 integer values." Each integer is four bytes, and thus you're allocating for four integers (4 bytes x 4 values) = 16 bytes of allocated memory.
no , n was equal to 100 in that example thus 100 * 4 = 400 bytes . int the next example she changed the value of n to 4 thus 4 * 4 = 16 bytes
random comment for engagement
Q. What is the correct way to allocate memory to store 12 double data?
A. malloc(20 * double)
B. (double*) malloc(12 * sizeof(double))
C. (double) malloc(20 * sizeof(double))
D. (double*) malloc( sizeof(20 * int))
B.
B
B
B
B. (double*) malloc(12 * sizeof(double))
helped a lot. thanks
best for beginner💓
Thank you very much
I'm getting a warning for ptr = realloc(ptr, n * sizeof(int)); saying that realloc could return NULL, which "will cause the original memory block to be leaked", any know how to fix this?
Thanks for the video, it helped a lot!
[ Programiz Quiz ]
Q. What is the correct way to allocate memory to store 12 double data?
a. malloc(20 * double)
b. (double*) malloc(12 * sizeof(double))
c. (double) malloc(20 * sizeof(double))
d. (double*) malloc( sizeof(20 * int))
The answer is b.
The answer is choice b , because you are allocating a block of memory on the heap for 12 elements , each element being a double.
1: You allocating 12* sizeof(double)
You can only use sizeof function on data types such as, sizeof(int), sizeof(float), sizeof(double)
2: and then you must type cast the void pointer to the block(array) of type double, so it can point to the right data type, in the above example,
you type cast it to (double*).
b
b
@@rickzalman4136 if the pointer was created as a double pointer, would you still need to type cast it to double?
@@errorhostnotfound1165
You need too!
// Example program in C
#include
#include // needed for
// malloc and free.
#define ROWS 4 // number of
// pointers to each array .
#define COLS 2 // number of
// elements in each array.
int main(){
double ** matrix; // type is double in this example.
// compiler needs this information
//
// each row(array) is a pointer to a
// pointer to an array of size COLS.
matrix= (double**)malloc(ROWS*sizeof(double*));
// Remember a multidimensional array is really an array which contains arrays.
int j, k ; // needed for loops
// next allocate each pointer in
// each row to a pointer which points to an array with a size of COLS.
//
for(j=0; j< ROWS; j ++)
matrix[i] = (double*)malloc(COLS*sizeof(double) );
// fill 2D array with values
for(j= 0; j< ROWS; j ++) // inner loop
for(k=0; k
Great video
mam , we should also use #include at the starting of code then only we are able to run the code
same here
Beauty with Brain🤗☺️
amazing teacher😍
That's true
Beauty with brain
Thanks!!!
shouldn't the stdlib.h header file be included when using the memory allocation functions?
When i run the realloc() code in visual studio code it returns an error as its expecting a void data type yet the malloc() worked.
Can anyone 'point' me in the right direction?
Thanks
int *ptr, i , n1, n2;
ptr = (int*) malloc(n1 * sizeof(int));
ptr = realloc(ptr, n2 * sizeof(int)); // error expecting void type in ptr ?
add biblio #include
Also put (int*) before realloc
nice
#include
#include
int main() {
int n, i, *ages;
n = 4;
ages = (int*) malloc(n * sizeof(int));
if(ages == NULL){
printf("ERROR! Memory not allocated
");
return 0;
}
printf("Input %d values:
", n);
for(i = 0; i < n; i++){
scanf("%d", ages + i);
}
printf("Initial array elements:
");
for(i = 0; i
isnt ages[4] basically the same with *(ages+4) ?
if return 0 terminates main function then what does return 1 do¿
Quiz answer should be B
Madam please what about calloc?
oui
I think the answer in B
opt B
ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT ! ENGAGEMENT !
Plz make course on C++.
Check there website they have course for c ,c++,java,python.
gud
B
Option B : (double*) malloc(12 * sizeof(double))
-----------------------------------------------------------------------------------------------------
#include
int main() {
int n = 4, i;
int* ages;
ages = (int*) malloc(n * sizeof(int));
if(ages == NULL) {
printf("Memmory Cannot Allocated");
return 0;
}
printf("Enter Four Ages:
");
for(i = 0; i < n; i++) {
scanf("%d", ages + i);
}
printf("Initial Array Elements:
");
for(i = 0; i < n; i++) {
printf("%d ", *(ages + i));
}
n = 6;
ages = realloc(ages, n * sizeof(int));
ages[4] = 32;
ages[5] = 59;
printf("
Final Array Elements:
");
for(i = 0; i
mam u r so pretty 😍
bobloc
#include
int main() {
int quantity = 4;
int* ages;
ages = (int*)malloc(quantity * sizeof(int));
if (ages == NULL) {
printf("Memory can`t be allocated");
return 0;
}
printf("Enter input values:
");
for (int i = 0; i < quantity; ++i) {
scanf("%d", ages + i);
}
printf("Input values:
");
for (int i = 0; i < quantity; ++i) {
printf("%d
", *(ages + i));
}
quantity = 6;
ages = realloc (ages, quantity * sizeof(int));
ages[4] = 32;
ages[5] = 59;
printf("We adding new values:
");
for (int i = 0; i < quantity; ++i) {
printf("%d
", *(ages + i));
}
free(ages);
return 0;
}
😌
❤❤
b
you are cute padma
programming task:
#include
#include
int main(){
int n=4;
int* ages;
if(ages==NULL){
printf("memory is not allocated");
}
ages=(int*)malloc(n*sizeof(int));
printf("enter input values: ");
for(int i=0;i
Aziz = ages😂
I finished the programming task and wanted to challenge myself by making it so the user decides how many spaces need to be added and what inputs should be added. I think I did it correctly but when I ran my code there was a "Segmentation Fault" which I don't know.
Code:
// Online C compiler to run C program online
#include
#include
int main() {
int n = 4;
int* ages;
int num1, num2, add;
ages = (int*) malloc(n*sizeof(int));
if(ages == NULL){
printf("ERROR! Memory not allocated
");
return 0;
}
printf("Enter your ages:
");
for(int i = 0; i
Are you Nepali?
free videos
Say Shazam😂😂
There no, code of Programming task. I want to know.. What's the Exact output.. Please Provide the code..
#include
int main() {
int n, i, *ages;
n = 4;
ages = (int*) malloc(n * sizeof(int));
if(ages == NULL){
printf("ERROR! Memory not allocated
");
return 0;
}
printf("Input %d values:
", n);
for(i = 0; i < n; i++){
scanf("%d", ages + i);
}
printf("Initial array elements:
");
for(i = 0; i
L0l0ß
#include
#include
int main() {
int n = 4;
int* age;
age = (int*)malloc(n * sizeof(int));
if (age == NULL) {
printf("Memory can not be allocated");
return 0;
}
printf("Enter four values for age:
");
for (int i = 0; i < n; ++i) {
scanf("%d", age + i);
}
printf("Four values of age are:
");
for (int i = 0; i < n; ++i) {
printf("%d
", *(age + i));
}
n = 6;
age = (int*)realloc(age, n * sizeof(int));
age[4] = 32;
age[5] = 59;
printf("All six age elements:
");
for (int i = 0; i < n; ++i) {
printf("%d
", age[i]);
}
free(age);
return 0;
}
# include
int main(){
int n = 4;
int* ages;
ages = (int*)malloc(n* sizeof(int));
if(ages == NULL){
printf("Memory cannot be allocated");
return 0;
}
printf("Enter input value
");
for(int i = 0; i < n; ++i){
scanf("%d",ages + i);
}
n = 6;
ages[4] = 32;
ages[5] = 59;
ages = realloc(ages,n* sizeof(int));
printf("Input value
");
for(int i = 0; i < n; ++i){
printf("%d
",*(ages + i));
}
free(ages);
return 0;
}
#include
#include
int main()
{
int n = 4;
int *ages;
ages = (int *)malloc(n * sizeof(int));
if (ages == NULL)
{
printf("Error allocating memory
");
return 0;
}
printf("Enter the value of n:
");
for (int i = 0; i < n; i++)
{
scanf("%d", ages + i);
}
printf("print the ages :
");
for (int i = 0; i < n; i++)
{
printf("%d ", *(ages + i));
}
printf("
let's resize the array for 6 input:
");
ages = realloc(ages, 6 * sizeof(int));
// preserve the 5 and 6 places for some specific values
*(ages + 4) = 32;
*(ages + 5) = 59;
printf("print the ages :
");
for (int i = 0; i < 6; i++)
{
printf("%d ", *(ages + i));
}
free(ages);
return 0;
}
#include
#include
int main() {
int n = 4;
int* ages;
ages = (int*) malloc(n * sizeof(int));
if (ages == NULL) {
printf("Memory cannot be allocated");
return 0;
}
printf("Enter input values:
");
for(int i = 0; i < n; i++) {
scanf("%d", ages + i);
}
printf("
Allocated Values:
");
for(int i = 0; i < n; i++) {
printf("%d
", *(ages + i));
}
n = 6;
ages[4] = 32;
ages[5] = 49;
ages = realloc (ages, n * sizeof(int));
printf("
Newly Allocated Values:
");
for(int i = 0; i < n; i++) {
printf("%d
", *(ages + i));
}
free(ages);
return 0;
}
#include
int main() {
int n;
int* ages;
n = 4;
ages = (int*) malloc(n * sizeof(int));
if(ages == NULL){
printf("Memory cannot be allocated
");
return 0;
}
printf("Input %d values:
", n);
for(int i = 0; i < n; i++){
scanf("%d", ages + i);
}
printf("first elements:
");
for(int i = 0; i
#include
#include
int main(){
int* ages;
int n = 4;
ages = (int*) malloc(n * sizeof(int));
if(ages == NULL){
printf("ERROR! Memory not allocated
");
return 0;
}
printf("Enter input values:
");
for (int i = 0; i < n; ++i) {
scanf("%d", ages + i);
}
printf("Insert all elements:
");
for (int i = 0; i < n; ++i ) {
printf("%d
", *(ages + i));
}
n = 6;
ages = realloc (ages, n * sizeof(int));
ages [4] = 32;
ages [5] = 59;
printf("new Values:
");
for (int i = 0; i < n; ++i){
printf("%d
", *(ages + i));
}
free(ages);
return 0;
}
#include
#include
int main()
{
int n = 4;
int* age=(int*)malloc(n*sizeof(int));
if(n==0){
printf("MEMORY IS NOT AVAILABLE");
return 0;
}
printf("ENTER FOUR AGES: ");
for(int i=0; i
#include
#include
int main() {
int* ages;
ages = (int*) malloc(4*sizeof(int));
for(int i=0;i
#include
#include
int main() {
int n = 4;
int* ages;
ages = (int*) malloc(n * sizeof(int));
if (ages == NULL) {
printf("Ages momory cannot be allocated");
return 0;
}
printf("Enter the ages:
");
for (int i = 0; i < n; ++i) {
scanf("%d/n", ages + i);
}
printf("Age:
");
for (int i = 0; i < n; ++i) {
printf("%d
", *(ages +i));
}
n = 6;
ages[4] = 32;
ages[5] = 59;
ages = realloc (ages, n * sizeof(int));
printf("New ages memory:
");
for (int i = 0; i < n; ++i) {
printf("%d
", *(ages + i));
}
free(ages);
return 0;
}
// Online C compiler to run C program online
#include
#include
int main() {
int* ages;
int n=4;
ages=(int*) malloc(n* sizeof(int));
printf("Enter input values:
");
for (int i=0; i
B
#include
#include
int main() {
int a = 4;
int* ages;
ages = (int *) malloc(a * sizeof(int));
printf("Enter input values for ages:
");
for (int i = 0; i < a; ++i) {
scanf("%d", ages + i);
}
printf("Input values of ages:
");
for (int i = 0; i < a; ++i) {
printf("%d
", *(ages + i));
}
a = 6;
ages = realloc(ages, a * sizeof(int));
ages[4] = 32;
ages[5] = 59;
printf("All the ages:
");
for(int i = 0; i < a; ++i) {
printf("%d = %p
", *(ages + i), ages + i);
}
free(ages);
return 0;
}
B is the correct answer.
#include
#include
int main(){
// Initialize
int* ages = NULL;
int var = 4;
// Allocate Memory
ages = (int*) malloc(var * sizeof (int));
if (ages == NULL){
printf("Memory cannot be allocated
");
return 0;
}
printf("Enter Values
");
for (int i = 0; i < var; i++){
scanf("%d", &ages[i]);
}
for (int i = 0; i < var; i++){
printf("%d
", ages[i]);
}
// Reallocate Memory
var = 6;
ages = realloc (ages, var * sizeof(int));
if (ages == NULL){
printf("Memory cannot be reallocated
");
return 0;
}
ages[4] = 32;
ages[5] = 59;
printf("Newly Allocated Memory
");
for(int i = 0; i < var; i++){
printf("%d
", ages[i]);
}
// Free Memory
free(ages);
return 0;
}
B
B