C Programming - Pointers - Discussion

Discussion Forum : Pointers - General Questions (Q.No. 2)
2.
Can you combine the following two statements into one?
char *p;
p = (char*) malloc(100);
char p = *malloc(100);
char *p = (char) malloc(100);
char *p = (char*)malloc(100);
char *p = (char *)(malloc*)(100);
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
82 comments Page 2 of 9.

LOIS said:   9 years ago
In C, 0 = true and others values represent false; then when one return 0 is to say that no errors occur.

Siva said:   9 years ago
Why are we using return 0 if we give the main function as int main? can anyone tell me?

Ganathantrapureddy said:   9 years ago
Thanks for all given solutions.

Pratik Dutta said:   9 years ago
We can variable declare as int i;

I=10;

Int i=10;

At first we declare the variable then assign value, but together we can write the last line.

Rishi said:   9 years ago
Explanations are good.

But one think is missing that since *p points to the beginning address, thus p=char* malloc(100), can be assigned with a char*p.

The type should be same is the necessary point but why so works is because of p(base address).

Raviranjan prasad said:   10 years ago
Because the char is a data type who declare a pointer type variable *p.

So it will used char *p and we will write any memorial allocation by (ptr=malloc (size in byte code).

When we change this type then write malloc by this syntax:

ptr = (data type *) malloc (size in byte code).

Ananda venkatesh said:   10 years ago
@Veena mitra.

int and float storage patterns are different. So, we get different outputs.

Ananda venkatesh said:   10 years ago
Because words[] is stored in code section and p is pointing to the address of heap section containing memory of sizeof (words). So, both the addresses are different.

Veena said:   1 decade ago
#include /* For standard input output */
#include /* You need this if on UNIX or LINUX */

int main()
{
char words[]={" this is a simple example for malloc\n"};
char *p; /* char pointer for use with malloc() */
int n=sizeof(words);
int i;
p = (char *)malloc(sizeof(words)); /* Explained in the following text */

printf("Base add of array word= %u",&words);
printf("\n address of pointer p= %u",p);


return(0);
}

The output is:

Base add of array word = 4294811566.

Address of pointer p = 139501688.

Here, why not p containing the base address of array words?

Veena Mitra said:   1 decade ago
Hi @Sandeep,

Can you explain me. If my program is :-

#include<stdio.h>
int main(){
float i=320;
int *ptr=(int*)&i;
printf("%d",*ptr);
return 0;
}

Then the answer is : 1134559232.

Why ?


Post your comments here:

Your comments will be displayed after verification.