473,595 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer and array

Hi,

Could please anyone clarify about pointer and array in C?

If I have:

int arr[10];

The following two commands will be the same: arr and &arr[0].

What about &arr? Is it not the same thing?
Nov 14 '05 #1
23 2711
Leon Brodskiy <iv**@rogers.co m> scribbled the following:
Hi, Could please anyone clarify about pointer and array in C? If I have: int arr[10]; The following two commands will be the same: arr and &arr[0].
They're expressions, not commands. But yes, they're the same thing,
in a value context.
What about &arr? Is it not the same thing?


Not the same thing at all. It's the address of the entire array, not
its first element. The most practical meaning of this is that such
addresses increment in terms of 10*sizeof(int), not in terms of
sizeof(int).

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
Nov 14 '05 #2
Thanks for your answer.
I'm asking because I have found in a program the following call:

void f1(char inp[10])
{...}

void main()
{...
char str[10];
....
f1(&str);
....
}

I tought this is a bug and this is not supposed to work but it does work.
Function receives pointer to char. I send to the function a pointer to a
pointer to char but still seems like in arrays it is the same pointer. So,
is this case str and &str the same?

Thanks.

"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:cq******** **@oravannahka. helsinki.fi...
Leon Brodskiy <iv**@rogers.co m> scribbled the following:
Hi,

Could please anyone clarify about pointer and array in C?

If I have:

int arr[10];

The following two commands will be the same: arr and &arr[0].


They're expressions, not commands. But yes, they're the same thing,
in a value context.
What about &arr? Is it not the same thing?


Not the same thing at all. It's the address of the entire array, not
its first element. The most practical meaning of this is that such
addresses increment in terms of 10*sizeof(int), not in terms of
sizeof(int).

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/

Nov 14 '05 #3
Leon Brodskiy <iv**@rogers.co m> scribbled the following:
Thanks for your answer.
I'm asking because I have found in a program the following call: void f1(char inp[10])
{...} void main()
Change this to int main(void).
{...
char str[10];
...
f1(&str);
...
} I tought this is a bug and this is not supposed to work but it does work.
Function receives pointer to char. I send to the function a pointer to a
pointer to char but still seems like in arrays it is the same pointer. So,
is this case str and &str the same?


That program is *NOT* correct. You are supposed to call f1(str), not
f1(&str). If the function f1 assigns its parameter to a char pointer
(char*), it might work under your implementation. But trying to access
the char array pointer (char(*)[]) as if it were a char pointer will
yield wrong results, mainly for the reasons I stated.

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
Nov 14 '05 #4
Joona I Palaste <pa*****@cc.hel sinki.fi> scribbled the following:
Leon Brodskiy <iv**@rogers.co m> scribbled the following:
Thanks for your answer.
I'm asking because I have found in a program the following call: void f1(char inp[10])
{...} void main()
Change this to int main(void). {...
char str[10];
...
f1(&str);
...
} I tought this is a bug and this is not supposed to work but it does work.
Function receives pointer to char. I send to the function a pointer to a
pointer to char but still seems like in arrays it is the same pointer. So,
is this case str and &str the same?

That program is *NOT* correct. You are supposed to call f1(str), not
f1(&str). If the function f1 assigns its parameter to a char pointer
(char*), it might work under your implementation. But trying to access
the char array pointer (char(*)[]) as if it were a char pointer will
yield wrong results, mainly for the reasons I stated.


I should not be answering C questions late in the evening. That program
is still wrong, but my answer above is wrong too. Any possible
assignment from &str to a char pointer happens *before* f1's code
begins, so what f1 does to the pointer does not matter. Does assigning
a char array pointer to a char pointer cause undefined behaviour?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra
Nov 14 '05 #5
If we leave char array pointer for a second, can we say that the program
will always work correctly?

Would it be correct to say that a difference between str and &str is that
str is char* (points to one single character) and &str is a pointer to an
array(points to array of 10 characters)? If this is a true then in the
program should not be any difference if we use &str or str - both of them
will send to the function the address of the first element in the array.

Thanks in advance.

"Joona I Palaste" <pa*****@cc.hel sinki.fi> wrote in message
news:cq******** **@oravannahka. helsinki.fi...
Joona I Palaste <pa*****@cc.hel sinki.fi> scribbled the following:
Leon Brodskiy <iv**@rogers.co m> scribbled the following:
Thanks for your answer.
I'm asking because I have found in a program the following call: void f1(char inp[10])
{...} void main()
Change this to int main(void). {...
char str[10];
...
f1(&str);
...
} I tought this is a bug and this is not supposed to work but it does work. Function receives pointer to char. I send to the function a pointer to a pointer to char but still seems like in arrays it is the same pointer. So, is this case str and &str the same?

That program is *NOT* correct. You are supposed to call f1(str), not
f1(&str). If the function f1 assigns its parameter to a char pointer
(char*), it might work under your implementation. But trying to access
the char array pointer (char(*)[]) as if it were a char pointer will
yield wrong results, mainly for the reasons I stated.


I should not be answering C questions late in the evening. That program
is still wrong, but my answer above is wrong too. Any possible
assignment from &str to a char pointer happens *before* f1's code
begins, so what f1 does to the pointer does not matter. Does assigning
a char array pointer to a char pointer cause undefined behaviour?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Remember: There are only three kinds of people - those who can count and

those who can't."
- Vampyra

Nov 14 '05 #6
Leon Brodskiy <iv**@rogers.co m> scribbled the following:
If we leave char array pointer for a second, can we say that the program
will always work correctly?
No. The C standard allows for an implementation to behave erroneously
when a pointer value is assigned to a variable of incompatible type.
Would it be correct to say that a difference between str and &str is that
str is char* (points to one single character) and &str is a pointer to an
array(points to array of 10 characters)?
Yes.
If this is a true then in the
program should not be any difference if we use &str or str - both of them
will send to the function the address of the first element in the array.


That is not true in the general case. At least I think it's not. Maybe
some of the real C gurus here can answer my original question about
whether it is undefined behaviour?

--
/-- Joona Palaste (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Parthenogeneti c procreation in humans will result in the founding of a new
religion."
- John Nordberg
Nov 14 '05 #7
"Leon Brodskiy" <iv**@rogers.co m> writes:
If we leave char array pointer for a second, can we say that the program
will always work correctly?

Would it be correct to say that a difference between str and &str is that
str is char* (points to one single character) and &str is a pointer to an
array(points to array of 10 characters)? If this is a true then in the
program should not be any difference if we use &str or str - both of them
will send to the function the address of the first element in the array.


Please don't top-post. Your response should follow any quoted text,
not precede it.

No we can't say that the program will always work correctly. A
pointer-to-char and a pointer-to-array-of-char are two distinct types.
On most systems, they happen to have the same representation and can
be used more or less interchangeably , but the language standard
doesn't guarantee this.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
Joona I Palaste wrote:
Leon Brodskiy <iv**@rogers.co m> scribbled the following:
If we leave char array pointer for a second, can we say that the program
will always work correctly?

No. The C standard allows for an implementation to behave erroneously
when a pointer value is assigned to a variable of incompatible type.

Would it be correct to say that a difference between str and &str is that
str is char* (points to one single character) and &str is a pointer to an
array(point s to array of 10 characters)?

Yes.

If this is a true then in the
program should not be any difference if we use &str or str - both of them
will send to the function the address of the first element in the array.

That is not true in the general case. At least I think it's not. Maybe
some of the real C gurus here can answer my original question about
whether it is undefined behaviour?


The two yield incompatible pointer types. I suppose a diagnostic is
required. I too await guru WRT undefined behavior.

--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9
Leon Brodskiy wrote:
Thanks for your answer.
I'm asking because I have found in a program the following call:

void f1(char inp[10])
{...}

void main()
A simple mistake. You meant:

int main(void)
{...
char str[10];
....
f1(&str);


This should be f1(str);

&str is a char (*)[10], not a char *, so it has the wrong type
for the f1() function.

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2341
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
204
12970
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 = {0,1,2,4,9};
28
2384
by: Wonder | last post by:
Hello, I'm confused by the pointer definition such as int *(p); It seems if the parenthesis close p, it defines only 3 integers. The star is just useless. It can be showed by my program: int main() {
1
3086
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
8
2222
by: Martin Jørgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
1
617
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a platform which has the following properties: 1) char's are 8-Bit. ( "char" is synomonous with "byte" ). 2) int's are 32-Bit. ( sizeof(int) == 4 ). 3) Pointers are 64-Bit. ( sizeof(int*) == 8 ).
17
3233
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ================================================================================ /* A function that returns a pointer-of-arrays to the calling function. */ #include <stdio.h> int *pfunc(void);
12
3867
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
42
5298
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
26
4838
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure about the syntax of calling the same. #include <stdio.h> void fp1()
0
7883
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8262
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8020
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8252
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3875
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2391
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.