473,750 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing array by a pointer

Hi,

I'm wondering if my small example is not
"dangerous" :

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?

Chris

Apr 6 '06 #1
10 1633

Christian Christmann wrote:
Hi,

I'm wondering if my small example is not
"dangerous" :

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the
No, you're not. Your `i` is of type `char`.
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?


No. Whichever way you look at it.

Apr 6 '06 #2
Vladimir S. Oka:
Christian Christmann:

{
char i; [...] I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the


No, you're not. Your `i` is of type `char`.


Which is an integer, see 6.2.5 Types - 4, 6 and 7.
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?


No. Whichever way you look at it.


Right, not even if i were of the integer type int. :-)

Jirka
Apr 6 '06 #3
Christian Christmann wrote:
Hi,

I'm wondering if my small example is not
"dangerous" :

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?


Say you altered your code - I've changed i from char to a long int - so
that, in the loop, the value assigned to *globalPtr is /more/ obviously
larger than a char.
#define SIZE 10

char global[SIZE];

char * globalPtr = global;

int main(void)
{
long int i;

for(i = 100000; i < 100009; ++i)
{
*globalPtr = i;

globalPtr++;
}

return 0;
}

On the first iteration, the value of i in binary is 110000110101000 00, and
assigning that to a char will result *not* in some memory overwrite, but a
loss of data, e.g. the char at address globalPtr would be perhaps have the
value 10100000, i.e., it got just CHAR_BITs worth of the long.
--
==============
Not a pedant
==============
Apr 6 '06 #4

Jirka Klaue wrote:
Vladimir S. Oka:
Christian Christmann:

{
char i; [...] I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the


No, you're not. Your `i` is of type `char`.


Which is an integer, see 6.2.5 Types - 4, 6 and 7.


Yes, of course, but if the OP knew that (or where to find it) he
wouldn't be asking the question. Being pedantic has its place and time.
I doubt this is either.
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?


No. Whichever way you look at it.


Right, not even if i were of the integer type int. :-)


My point exactly.

Apr 6 '06 #5
>> #define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()


Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}


As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the


No, you're not. Your `i` is of type `char`.


Sorry, 'char i' was supposed to be 'int i' ;-)
Apr 6 '06 #6

Christian Christmann wrote:
#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()


Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}


As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the


No, you're not. Your `i` is of type `char`.


Sorry, 'char i' was supposed to be 'int i' ;-)


Yet another good reason for oft repeated request for the code to be
pasted in, not typed in. Still, my original reply holds. You're also
advised to read pemo's.

Apr 6 '06 #7
Christian Christmann wrote:
#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()


Spell it out:

int main(void)
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}


As `main` returns an `int` so should you:

return 0;
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the


No, you're not. Your `i` is of type `char`.


Sorry, 'char i' was supposed to be 'int i' ;-)


Since the int values happen to be within the minimum
ranges of char (0 to 127),
there are no complications with the conversion.
Converting an out of range value to type char
is implementation defined.

Assignment is according to value, not representation.
In an assignment operation,
if the left and right operands have different types
then the right operand must be converted to the type
of the left.

If you have

int i = 0; /* or any other int value */
char g;

Then the only difference between

g = i;

and

g = (char)i;

is that the first one is more likely to generate a warning.
The two statements have the same meaning.
They're both expression statements and
both expressions have the same value
and the same side effects.

--
pete
Apr 6 '06 #8

Christian Christmann wrote:
Hi,

I'm wondering if my small example is not
"dangerous" :

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?

Chris


memset()

Apr 6 '06 #9
Ivanna Pee wrote:

Christian Christmann wrote:
Hi,

I'm wondering if my small example is not
"dangerous" :

#define SIZE 10
char global[SIZE];
char* globalPtr = global;

int main()
{
char i;

for ( i = 0; i < SIZE; i = i + 1 ) {
(*globalPtr) = i;
globalPtr++;
}
}

I'm initializing the char array 'global' with
integer values (represented by 'i'). But since the
integer data type is 4x larger (4 byte on my machine) than
the char data type, I initialize the char element but at
the same time I overwrite the 3 subsequent char array elements
by the remaining 3 integer bytes. Moreover, when initializing
the last array element, I write 3 bytes beyond the array ranges
which might corrupt some other values stored in memory.
Is this right?

Chris


memset()


memset has nothing to do with the original post.

--
pete
Apr 6 '06 #10

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

Similar topics

27
4232
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just the first 8-bits for example. For me, I think it would be best if I can declare the 32-bits like this: unsigned char bits;
9
2341
by: sakitah | last post by:
Here is what I have: struct SubList { int BookId; int WFreq; }; struct Listing {
14
6338
by: Thes | last post by:
Hi all, I have a 2d float array to which I have declared a pointer thus: float Matrix = {/* snipped initialisation */ }; float (*StoredMatrix); So as to create a copy in a program "registry" of my own devising.
5
9289
by: junky_fellow | last post by:
Consider a 3-dimensional array: char a = { "abcd", "efgh", "ijkl" , "mnop", "qrst", "uvwx" } ; when i print the addresses for a,a and a they are same. but when i access element a+1, a+1 and a+1 they are different. why is it so?
8
1868
by: Dawn Minnis | last post by:
Hey guys If I have a program (see codeSnippet1) that I compile to be called test.o Then run it as test.o n n 2 3 4 I want the code to be able to strip out the two characters at the start (always going to be 2) and store them as characters. But I can't seem to get it to work because it is a pointer to a vector of characters. However, if I only run with integer arguements and use codeSnippet2 it works fine and they convert nicely to...
6
2081
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
0
1982
by: harsha1305 | last post by:
Hi all, I need to create a pointer to array of structure. Definition of structure: typedef struct { char b_name; unsigned long int sig; unsigned long int count; volatile unsigned char *Su_buffer;
38
3005
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've managed to create arrays using DOUBLE data types, but when I try to access the array, the compiler complains that the number is not an INT, i.e.
2
2290
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
16
6794
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
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
9568
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...
0
9256
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
8257
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...
1
6801
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4881
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3320
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
2
2794
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.