473,412 Members | 2,284 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,412 software developers and data experts.

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 1611

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 11000011010100000, 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
Christian Christmann <pl*****@yahoo.de> writes:
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?


A simple assignment, such as your "(*globalPtr) = i;", simply copies a
value (determined by evaluating the right hand side) to an object
(determined by the left hand side). The expression is converted, if
necessary, to the target type; it will never write any data outside
the target object.

You said elsewhere that you meant i to be an int, not a char. That
just means that the value is converted from int to char. There could
be problems with the conversion if the value is to large to fit in a
char, but the worst thing that can happen is that some
implementation-defined value is assigned to the target (or, in C99,
that an implementation-defined signal is raised); it will never
clobber memory outside the target object. (Any problems occur on the
conversion, not on the assignment.)

--
Keith Thompson (The_Other_Keith) 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.
Apr 6 '06 #11

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

Similar topics

27
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...
9
by: sakitah | last post by:
Here is what I have: struct SubList { int BookId; int WFreq; }; struct Listing {
14
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...
5
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...
8
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...
6
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
0
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...
38
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...
2
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
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) {
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.