473,382 Members | 1,689 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,382 software developers and data experts.

unexpected array modification

I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 4

int main(void)
{

int idx;
unsigned char bit[SIZE];

char tmpstr[1];

/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

strcpy(tmpstr, "0"); // why does this have any affect?

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

return 0;
}

Makefile:

CC = gcc
CFLAGS = -Wall

all: test

test: test.c
$(CC) $(CFLAGS) -o $@ $<

clean:
rm -f *.o test

..PHONY: clean

Sep 9 '06 #1
4 1502
j_hop_97 wrote:
I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 4

int main(void)
{

int idx;
unsigned char bit[SIZE];

char tmpstr[1];

/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

printf("\n\n");
for (idx = 0;idx < SIZE; idx++) {
printf("%d. bit[%d] = '%d'\n", idx, idx, bit[idx]);
}

strcpy(tmpstr, "0"); // why does this have any affect?
Because tmpstr only has a size of one, but you are writing two
characters to here, (a '0' and a '\0'). It sounds like
the '\0' is being written to bit[0]. Try making tmpstr have
size 2, and the problem will probably go away.

--
Bill Pursell

Sep 9 '06 #2
j_hop_97 wrote:
I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".
[...]
char tmpstr[1];
[...]
strcpy(tmpstr, "0"); // why does this have any affect?
This will try to store two characters in tmpstr: '0', and '\0'. That
final '\0' is the only way C standard library functions can know how
long a string is. You only have enough room for a single character, so
the '\0' gets stored in whatever happens to follow tmpstr in memory.
(You may even get the program to exit, or worse, especially if nothing
directly follows tmpstr in memory.)

Sep 9 '06 #3

Harald van Dijk wrote:
j_hop_97 wrote:
I'm hoping someone can explain the behavior of the following program.
When I run it on my linux machine the value of "bit[0]" for the last
"for" loop is "0".
[...]
char tmpstr[1];
[...]
strcpy(tmpstr, "0"); // why does this have any affect?

This will try to store two characters in tmpstr: '0', and '\0'. That
final '\0' is the only way C standard library functions can know how
long a string is. You only have enough room for a single character, so
the '\0' gets stored in whatever happens to follow tmpstr in memory.
(You may even get the program to exit, or worse, especially if nothing
directly follows tmpstr in memory.)
Yep, that was it. Defining tmpstr as "char tmpstr[2]" solved the
problem. Thanks.

Sep 9 '06 #4
Op 9 Sep 2006 09:10:07 -0700 schreef j_hop_97:

<snip>
/* initialize the array*/
for (idx = 0;idx < SIZE; idx++) {
bit[idx] = (unsigned char)idx + 1;
Lose the cast, it's unnecessary here, as most casts are.
--
Coos
Sep 9 '06 #5

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

Similar topics

1
by: Heinz | last post by:
Hello, I have a project that is used by other project in my solution. This common project is also used by a VB6 application (so it is placed in GAC). Since I did this (this is what I...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
5
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
2
by: RenTechie | last post by:
I get this error: Parse error: parse error, unexpected $ in on line 63 Where is the path and filename for a file containing: <?php function cityname($style="10engdir eng 50engloc"){//creates...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
5
by: Joshr35 | last post by:
I keep getting this same error on my store front page login area. Parse error: syntax error, unexpected '{' Here's the actuall php script: <?php if ( (!strstr($_SERVER,'login.php')) and...
37
by: Richard Heathfield | last post by:
candide said: They aren't. An array is an array. An address is a pointer value. These are not the same thing. If you mean that &array and &array are the same, they aren't. They have different...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.