473,545 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference for array in c and c++?

hi friends,
is ther any difference in array in c and array in c++?

Sep 25 '07 #1
45 7332
user923005 said:
On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
>hi friends,
is ther any difference in array in c and array in c++?

Sure, if it's an array of objects.
In both C and C++, there's no other kind of array than an array of objects
(in the C sense of the word, which is also as near as makes no odds the
C++ sense of the word).

<snip>

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 25 '07 #2
user923005 wrote:
On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
>hi friends,
is ther any difference in array in c and array in c++?

Sure, if it's an array of objects.
Only if those are C++ types, if the code compiles as C, there isn't any
difference.

--
Ian Collins.
Sep 25 '07 #3
On Sep 25, 12:21 am, Ian Collins <ian-n...@hotmail.co mwrote:
user923005 wrote:
On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
hi friends,
is ther any difference in array in c and array in c++?
Sure, if it's an array of objects.

Only if those are C++ types, if the code compiles as C, there isn't any
difference.
Besides the difference for allocated memory management, the public
symbol names can also be different. C++ data types can be decorated
with name mangling stuff. So if you link to a public symbol compiled
in C and your program is C++ you may have to define the linkage with
extern "C".

Sep 25 '07 #4
On Sep 25, 12:06 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
user923005 said:
On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
hi friends,
is ther any difference in array in c and array in c++?
Sure, if it's an array of objects.

In both C and C++, there's no other kind of array than an array of objects
(in the C sense of the word, which is also as near as makes no odds the
C++ sense of the word).

<snip>
In C++ the array operator [] can be overloaded to mean something
completely different than what it means in C.

Sep 25 '07 #5
user923005 wrote:
On Sep 25, 12:21 am, Ian Collins <ian-n...@hotmail.co mwrote:
>user923005 wrote:
>>On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
hi friends,
is ther any difference in array in c and array in c++?
Sure, if it's an array of objects.
Only if those are C++ types, if the code compiles as C, there isn't any
difference.

Besides the difference for allocated memory management, the public
symbol names can also be different. C++ data types can be decorated
with name mangling stuff. So if you link to a public symbol compiled
in C and your program is C++ you may have to define the linkage with
extern "C".
There need not be any difference for allocated memory management, malloc
works in C++. Name mangling applies to functions, not arrays.

I think the OP was asking about behavior of arrays, not the objects
stored in them, or how the memory they use is allocated.

--
Ian Collins.
Sep 25 '07 #6
user923005 said:
On Sep 25, 12:06 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
>user923005 said:
On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
hi friends,
is ther any difference in array in c and array in c++?
Sure, if it's an array of objects.

In both C and C++, there's no other kind of array than an array of
objects (in the C sense of the word, which is also as near as makes no
odds the C++ sense of the word).

<snip>

In C++ the array operator [] can be overloaded to mean something
completely different than what it means in C.
Of course, but that doesn't change the nature of arrays. It merely changes
the nature of the [] operator. And my point remains - that *all* arrays,
in both C and C++, are arrays of objects. So to say "if it's an array of
objects" is tautologically and redundantly superfluous.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Sep 25 '07 #7
On Sep 25, 1:37 am, Ian Collins <ian-n...@hotmail.co mwrote:
user923005 wrote:
On Sep 25, 12:21 am, Ian Collins <ian-n...@hotmail.co mwrote:
user923005 wrote:
On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
hi friends,
is ther any difference in array in c and array in c++?
Sure, if it's an array of objects.
Only if those are C++ types, if the code compiles as C, there isn't any
difference.
Besides the difference for allocated memory management, the public
symbol names can also be different. C++ data types can be decorated
with name mangling stuff. So if you link to a public symbol compiled
in C and your program is C++ you may have to define the linkage with
extern "C".

There need not be any difference for allocated memory management, malloc
works in C++. Name mangling applies to functions, not arrays.

I think the OP was asking about behavior of arrays, not the objects
stored in them, or how the memory they use is allocated.
C:\tmp>cl test.c t.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

test.c
Generating Code...
Compiling...
t.cpp
Generating Code...
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj
t.obj
test.obj : error LNK2019: unresolved external symbol _uiCpp referenced
in function _main
test.exe : fatal error LNK1120: 1 unresolved externals

C:\tmp>type test.c
#include <stdio.h>

extern unsigned int uiCpp[4];

int main(void)
{
printf("uiCpp[0] = %u\n", uiCpp[0]);
return 0;
}

C:\tmp>type t.cpp
unsigned int uiCpp[4] = {1};

C:\tmp>ren t.cpp t.c

C:\tmp>cl test.c t.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

test.c
t.c
Generating Code...
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:test.exe
test.obj
t.obj

C:\tmp>

Sep 25 '07 #8
anto_frank:
hi friends,
is ther any difference in array in c and array in c++?

I program in both languages are there's no suprises when it comes to
arrays.

There's a good page on the web that has a finite list of all the
differences between C and C++ when it comes to their common subset...
can't remember the URL though, try Google for something like
"Difference s C C++". Off the top of my head, here's a few of the
differences:

1: Character literals are int in C, char in C++.
2: Global variable are static in C, extern in C++ (...if I remember
correctly)

Martin
Sep 25 '07 #9
On Sep 25, 1:58 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
user923005 said:


On Sep 25, 12:06 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
user923005 said:
On Sep 24, 11:20 pm, anto frank <j.antofrank... @gmail.comwrote :
hi friends,
is ther any difference in array in c and array in c++?
Sure, if it's an array of objects.
In both C and C++, there's no other kind of array than an array of
objects (in the C sense of the word, which is also as near as makes no
odds the C++ sense of the word).
<snip>
In C++ the array operator [] can be overloaded to mean something
completely different than what it means in C.

Of course, but that doesn't change the nature of arrays. It merely changes
the nature of the [] operator. And my point remains - that *all* arrays,
in both C and C++, are arrays of objects. So to say "if it's an array of
objects" is tautologically and redundantly superfluous.
My point was that you should not count on a C++ array behaving like a
C array (it may or may not behave in a similar way). The objects in C+
+ are also (generally) quite different than C objects, which do not
have constructors or destructors.

Assigning an object to an array can have a very different meaning for
a struct, for instance.
In C++, the struct can have a constructor, and in C it cannot.
In short, there are subtle and important differences between arrays in
C and C++.
They are not the same.
C:\tmp\p\new>cl t.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

t.cpp
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:t.exe
t.obj

C:\tmp\p\new>t
arr[2].a=5, arr[2].b=5,arr[2].x=3.571429

C:\tmp\p\new>ty pe t.cpp
#include <stdio.h>

struct foo {
int a;
int b;
double x;
foo() {
a = 5;
b = 5;
x = (a * b) / 7.0;
}
};

int main()
{
struct foo arr[10];
printf("arr[2].a=%d, arr[2].b=%d,arr[2].x=%f\n", arr[2].a,
arr[2].b, arr[2].x);
return 0;
}

Sep 25 '07 #10

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

Similar topics

11
5696
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little difference between a struct and a class in C++. Both have inheritance, access modifiers, etc. The only diff I see is the default access level is public...
10
5097
by: David | last post by:
what's the differences between: int main(int argc,char* argv){ ... } and: int main(int argc,char** argv){ ...
15
2221
by: Mohanasundaram | last post by:
Hi All, What is the difference between malloc and calloc other than the point that calloc will initialize the memory to all zeros? This was an interview question for me. All the books and references that I have come across tells that calloc initialize the memory to all zeros. In Herbert Shiltd's C/C++ reference I found that calloc returns a...
18
2231
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
7
3439
by: iamchiaweilin | last post by:
Hello all: What's the difference between p and q in the following statements? char p = "Hello"; char *q = "Hello"; I know q stores the address of 'H'. Question is: does p store the address of 'H' too?
33
2232
by: onkar | last post by:
#include<stdio.h> int main(int argc,char **argv){ int a={1,2,3,4,5}; printf("%p\n%p\n",a,&a); return 0; } gives me : 0xbfe837a0
5
4565
by: rajm2019 | last post by:
hi all, i want to know that what is the actual difference b/w the character array & character pointer.then how u will get the addrees of a char array char str="be silent like u" char *p1="be eloquent r u" char *p2; p2=str;
10
3417
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
11
12091
by: cmb3587 | last post by:
I have two arrays and I'm trying to create a 3rd array that is the difference between the two arrays Ex: arrayA: 3 5 8 9 arrayB: 3 4 6 9 difference of A-B: 5 8 however, my code is just returning me an array of 0's
0
7406
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...
0
7813
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7431
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...
0
5976
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...
1
5337
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
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...
0
3457
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...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.