473,322 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

pointers

I am having a hard time with this:

Which statement is true given the following declaration:

int n=5, *p=&n;

a) p is a pointer initialized to point to n.
b) p is a pointer initialized to the value 5.
c) n and p are both pointer variables.
d) The declaration contains a syntax error.
Nov 13 '07 #1
3 991
beacon
579 512MB
Think of a pointer as a variable that has to have an asterisk before it. That's the only difference between the two.

If n is an integer, it will hold an integer value.

If *p is declared as an integer, it will hold the address for an integer value, which means that when *p is dereferenced, you will get an integer returned.

However, even though n and *p are both integers, they have to be declared separately just like a float and a double would, or how a string and a character would.

Does this clear it up a little?
Nov 13 '07 #2
drhowarddrfine
7,435 Expert 4TB
Assuming *p is declared as a pointer to an integer:
int *p;
you are saying p points at an integer.

Saying *p=&n is saying make the integer p is pointing at equal to & (the address of) the integer n. But p is supposed to point to an integer so this is incorrect.
Nov 14 '07 #3
Banfa
9,065 Expert Mod 8TB
Saying *p=&n is saying make the integer p is pointing at equal to & (the address of) the integer n. But p is supposed to point to an integer so this is incorrect.
Not in initialisation

Expand|Select|Wrap|Line Numbers
  1. int n=5;
  2. int *p;
  3.  
  4. *p = &n;
  5.  
In this example p is declared and then not initialised. This code says make the int p points at equal to the address of the integer n (probably but not always wrong).

Expand|Select|Wrap|Line Numbers
  1. int n=5;
  2. int *p = &n;
  3.  
In this code p is declared and initialised at the same time. This says p is a pointer to an integer and it's value is the address of the integer n.

These 2 code sames result in different assignments, the first assigns to *p and the second assigns to p.
Nov 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.