Operating System - HP-UX
1755691 Members
3139 Online
108837 Solutions
New Discussion юеВ

How to use malloc to create an array of char[15]

 
Xiaoming Zhang
Contributor

How to use malloc to create an array of char[15]

Is anyone know how to use malloc funciton to create an array of char[150]?

Thanks.

Xiaoming
2 REPLIES 2
Madhu Sudhan_1
Respected Contributor

Re: How to use malloc to create an array of char[15]

Hi :

main() {
char *ptr;
ptr = (char *)malloc(150 * sizeof(char));
/* ptr holds the starting address of the array */
/* use pointer arithmetic from here on */
}

Enjoy !
...Madhu
Think Positive
Xiaoming Zhang
Contributor

Re: How to use malloc to create an array of char[15]

Hi,

The reply does work in one function. When passing back to caller function, accessing the strings assigned in the called function gave different output in caller function.

I created an array of struct which contains a variable like char fileName[150] and it work fine when it is passed back to caller function.

Thank you any way.

Xiaoming