473,766 Members | 2,093 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
10 1637
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 "(*globalPt r) = 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_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.
Apr 6 '06 #11

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

Similar topics

27
4236
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
2342
by: sakitah | last post by:
Here is what I have: struct SubList { int BookId; int WFreq; }; struct Listing {
14
6340
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
9293
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
1869
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
2082
by: archilleswaterland | last post by:
structures typedef struct{ char name; int age; float balance; }account; account xyx; accout *ptr;
0
1984
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
3010
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
2293
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
6797
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
9404
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
10168
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
10008
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
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
9837
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
8833
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
6651
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();...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.