Previous in Forum: Tetrakaidecahedron Model or Kelvin Unit Cell   Next in Forum: PCS7 vs TIA
Close
Close
Close
7 comments
Rate Comments: Nested
Associate

Join Date: Apr 2017
Posts: 28
Good Answers: 1

Pointer Question

06/23/2018 1:37 AM

i dont understand what diffirent and meaning of (char*)x char *x (char*)&x ..please help me, an example following output is much better.

Register to Reply
Interested in this topic? By joining CR4 you can "subscribe" to
this discussion and receive notification when new comments are added.
Guru

Join Date: Mar 2007
Location: by the beach in Florida
Posts: 33392
Good Answers: 1817
#1

Re: pointer question

06/23/2018 2:08 AM
__________________
All living things seek to control their own destiny....this is the purpose of life
Register to Reply
Guru

Join Date: Mar 2007
Location: by the beach in Florida
Posts: 33392
Good Answers: 1817
#2
In reply to #1

Re: pointer question

06/23/2018 2:28 AM

__________________
All living things seek to control their own destiny....this is the purpose of life
Register to Reply
Guru

Join Date: Mar 2007
Location: by the beach in Florida
Posts: 33392
Good Answers: 1817
#3
In reply to #2

Re: pointer question

06/23/2018 2:51 AM

char* is a pointer to the first char in the return "string" (char array). although the size of the array isn't given, in C "string"s are null terminated. meaning you can start reading the chars of the position the pointer is set to until you encounter a null char('\0').

https://stackoverflow.com/questions/9995889/what-does-char-do-or-mean-as-a-return-type-for-a-function

__________________
All living things seek to control their own destiny....this is the purpose of life
Register to Reply
Guru

Join Date: Apr 2010
Location: About 4000 miles from the center of the earth (+/-100 mi)
Posts: 9921
Good Answers: 1142
#4

Re: pointer question

06/23/2018 8:02 AM

In C, different types of data have different size. For example, a character may take 1 byte, a short integer 2 bytes, a regular integer 4 bytes, a floating point number 4 bytes, etc. You have to tell the compiler what kind of number a variable is so that storage can be arranged.

You can also have a null pointer, which tells the compiler that the variable is a pointer which you can assign a size later. If x is a null pointer, you can change it to a char pointer with (char*)x.

To declare x as a character pointer, you assign it as char *x.

If you want to find the address of a variable x, the use &x. To make that address an address to store a character, cast it as a character pointer (char*)&x.

I hope this helps...

Register to Reply
Guru

Join Date: Apr 2010
Location: About 4000 miles from the center of the earth (+/-100 mi)
Posts: 9921
Good Answers: 1142
#5

Re: Pointer Question

06/23/2018 2:00 PM

The stack is a place in memory where the computer stores temporary data, for example, its current address and variables it is working on when interrupted to do another task. It's usually a program error that causes this area to fill up, hence "stack overflow".

A pointer is a variable that holds an address. The address can be a character, character string, number, structure, etc. An array is a group of characters, numbers, etc. C refers to arrays by their addresses so that it doesn't have to move a lot of data around, hence the confusion between pointers and arrays.

Register to Reply
Guru

Join Date: Mar 2007
Location: by the beach in Florida
Posts: 33392
Good Answers: 1817
#7
In reply to #5

Re: Pointer Question

06/24/2018 12:50 PM

pedro54 is a spam bot....haha

__________________
All living things seek to control their own destiny....this is the purpose of life
Register to Reply
Guru

Join Date: Oct 2009
Posts: 1460
Good Answers: 30
#6

Re: Pointer Question

06/24/2018 8:21 AM

char* x, char *x,char*x, (char*) x and (char *) x all mean the same, declaring x as a variable of type address, the address being the address of some other variable. &x is the address of the variable x, so you could meaningfully write

char* y = &x;

I hope it is then clear that (char*) &x is not meaningful, as the & itself declares an address.

The bracketed forms are used when "casting", i.e. assigning the value of a variable to another variable of a different but compatible type, e.g.

char* x[] = "ABCD";

char* y = &x; /* the address of x, being that cell in RAM where the A is stored */

int z = (int) y; /* the integer value of the address of x */

char* a = (char*) z; /* a now holds the address of x, and is equal to y */

Register to Reply
Register to Reply 7 comments
Copy to Clipboard

Users who posted comments:

phph001 (1); Rixter (2); SolarEagle (4)

Previous in Forum: Tetrakaidecahedron Model or Kelvin Unit Cell   Next in Forum: PCS7 vs TIA

Advertisement