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

ansi C 2 dim array

Hi guys,
Thanks in advance and any help is greatly appreciated. am having a problem saving into my 2 dim char array, I am trying to find out why does the second time I strcpy the new value to tokenAttributeName & tokenAttribute it overrides the value of the first saved values, here's how the code goes: (the display logs are at the bottom)

char *TaskAndPercentages[5][2];

Action4()
{
char tokenAttributeNames[1000];
char tokenAttributes[1000];

char temptokenAttributeNames[1000] = "name1";
char temptokenAttributes[1000] = "100";

strcpy(tokenAttributeName, temptokenAttributeNames);
strcpy(tokenAttribute, temptokenAttributes);

TaskAndPercentages[0][0] = tokenAttributeName;
TaskAndPercentages[0][1] = tokenAttribute;

displayvalues();

strcpy(temptokenAttributeNames, "BBB");
strcpy(temptokenAttributes, "99");

displayvalues();

strcpy(tokenAttributeName, temptokenAttributeNames);
strcpy(tokenAttribute, temptokenAttributes);

displayvalues();

TaskAndPercentages[1][0] = tokenAttributeName;
TaskAndPercentages[1][1] = tokenAttribute;



return 0;
}

displayvalues()
{
lr_output_message ("The TaskAndPercentages[0][0] = %s", TaskAndPercentages[0][0] );
lr_output_message ("The TaskAndPercentages[0][1] = %s", TaskAndPercentages[0][1] );
lr_output_message ("The TaskAndPercentages[1][0] = %s", TaskAndPercentages[1][0] );
lr_output_message ("The TaskAndPercentages[1][1] = %s", TaskAndPercentages[1][1] );
lr_output_message ("The TaskAndPercentages[2][0] = %s", TaskAndPercentages[2][0] );
lr_output_message ("The TaskAndPercentages[2][1] = %s", TaskAndPercentages[2][1] );
lr_output_message ("The TaskAndPercentages[3][0] = %s", TaskAndPercentages[3][0] );
lr_output_message ("The TaskAndPercentages[3][1] = %s", TaskAndPercentages[3][1] );
lr_output_message ("The TaskAndPercentages[4][0] = %s", TaskAndPercentages[4][0] );
lr_output_message ("The TaskAndPercentages[4][1] = %s", TaskAndPercentages[4][1] );

return 0;
}


log displayed:
Action4.c(39): The TaskAndPercentages[0][0] = name1
Action4.c(40): The TaskAndPercentages[0][1] = 100
Action4.c(41): The TaskAndPercentages[1][0] = (null)
Action4.c(42): The TaskAndPercentages[1][1] = (null)
Action4.c(43): The TaskAndPercentages[2][0] = (null)
Action4.c(44): The TaskAndPercentages[2][1] = (null)
Action4.c(45): The TaskAndPercentages[3][0] = (null)
Action4.c(46): The TaskAndPercentages[3][1] = (null)
Action4.c(47): The TaskAndPercentages[4][0] = (null)
Action4.c(48): The TaskAndPercentages[4][1] = (null)
Action4.c(39): The TaskAndPercentages[0][0] = name1
Action4.c(40): The TaskAndPercentages[0][1] = 100
Action4.c(41): The TaskAndPercentages[1][0] = (null)
Action4.c(42): The TaskAndPercentages[1][1] = (null)
Action4.c(43): The TaskAndPercentages[2][0] = (null)
Action4.c(44): The TaskAndPercentages[2][1] = (null)
Action4.c(45): The TaskAndPercentages[3][0] = (null)
Action4.c(46): The TaskAndPercentages[3][1] = (null)
Action4.c(47): The TaskAndPercentages[4][0] = (null)
Action4.c(48): The TaskAndPercentages[4][1] = (null)
Action4.c(39): The TaskAndPercentages[0][0] = BBB
Action4.c(40): The TaskAndPercentages[0][1] = 99
Action4.c(41): The TaskAndPercentages[1][0] = (null)
Action4.c(42): The TaskAndPercentages[1][1] = (null)
Action4.c(43): The TaskAndPercentages[2][0] = (null)
Action4.c(44): The TaskAndPercentages[2][1] = (null)
Action4.c(45): The TaskAndPercentages[3][0] = (null)
Action4.c(46): The TaskAndPercentages[3][1] = (null)
Action4.c(47): The TaskAndPercentages[4][0] = (null)
Action4.c(48): The TaskAndPercentages[4][1] = (null)
May 7 '10 #1
1 2374
donbock
2,426 Expert 2GB
Please use CODE tags when you post code. That would have provided line numbers that I could refer to.

Each entry in your 2-dimensional array is a pointer to a string that is stored someplace else. Notice that the strings themselves are not stored in the array. The strcpy's alter the strings (which are stored elsewhere), not the array.

Look at the last two lines in Action4 before the return statement where you assign values to array entries [1][0] and [1][1]. At this point, both [0][0] and [1][0] point at the same string (tokenAttributeName); and both [0][1] and [1][1] point at the same string (tokenAttribute). Were you to call displayvalues just before the return you would get
Expand|Select|Wrap|Line Numbers
  1. Action4.c(39): The TaskAndPercentages[0][0] = BBB
  2. Action4.c(40): The TaskAndPercentages[0][1] = 99
  3. Action4.c(41): The TaskAndPercentages[1][0] = BBB
  4. Action4.c(42): The TaskAndPercentages[1][1] = 99
  5. Action4.c(43): The TaskAndPercentages[2][0] = (null)
  6. Action4.c(44): The TaskAndPercentages[2][1] = (null)
  7. Action4.c(45): The TaskAndPercentages[3][0] = (null)
  8. Action4.c(46): The TaskAndPercentages[3][1] = (null)
  9. Action4.c(47): The TaskAndPercentages[4][0] = (null)
  10. Action4.c(48): The TaskAndPercentages[4][1] = (null) 
If you want each array entry to point to a unique string then you need distinct string buffers for each to point at ... something like this:
Expand|Select|Wrap|Line Numbers
  1. char buf00[1000];
  2. char buf01[1000];
  3. ...
  4. char buf41[1000];
Followed by one of the following two lines. I don't trust myself to remember the organization of multidimensional arrays, so I always look it up. It is up to you to look it up and choose the right initializer.
Expand|Select|Wrap|Line Numbers
  1. char * const TaskAndPercentages[5][2] =
  2.     { {buf00, buf01}, ... {buf40, buf41} };
  3. <<or>>
  4. char * const TaskAndPercentages[5][2] =
  5.    { {buf00, buf10, ... buf40}, {buf01, buf02, ... buf41} };
Notice how I use const here. This makes the pointers constant -- you can't change which string buffers they point at.

Alternatively, you could include the string buffers in the array. As I mentioned above, I would have to look it up to decide which of these is correct:
Expand|Select|Wrap|Line Numbers
  1. char TaskAndPercentages[5][2][1000];
  2. <<or>>
  3. char TaskAndPercentages [1000][5][2];
I don't recommend either of these approaches though. It is much more straightforward to use a one-dimensional array of structures. No need to look up the details of array organization; and no need to remember that [0] is the name and [1] is the attributes.
Expand|Select|Wrap|Line Numbers
  1. struct tt {
  2.    char name[1000];
  3.    char attribute[1000];
  4.    };
  5. struct tt TaskAndPercentages[5];
May 8 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Salisha Khan | last post by:
I have an instance method that takes an ansi string. Dim foo As Object foo(String a) The problem is Strings in VB .NET are unicode while in order for the method to work it must accept an...
8
by: Hagen | last post by:
Hi, I have a question that you probably shouldn´t worry about since the compiler cares for it, but anyways: When you run your compiler with optimization turned on (eg. g++ with -Ox flag) and...
13
by: HappyHippy | last post by:
Hi, I'm wondering what you think about this piece of code: #include<iostream> int main() { int size; std::cin >> size;
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
4
by: Anonymousgoogledeja | last post by:
hi all, while I read the code from linux sources. I read the statement static const struct iw_ioctl_description standard_ioctl = { = { .header_type = IW_HEADER_TYPE_NULL, },
4
by: Julia | last post by:
Hi, I need to convert unicode string to ansi string Thanks in adavance.
7
by: Paul Connolly | last post by:
char *s = "Hello"; s = 'J'; puts(s); might print "Jello" in a pre-ANSI compiler - is the behaviour of this program undefined in any pre-ANSI compiler - or would it always have printed "Jello"...
6
by: msdnuniv | last post by:
Hello everybody, since days i try to convert Unicode-Strings in VB.NET to ANSI which should be processable in VB6 and converted to unicode again. It should be possible with any codepage, e.g....
7
by: Heriberto Bens | last post by:
Hi, friends. I've seen the following code in an example: int fun(int n) { int i, x; for (i = 0; i < n; i++) x = 5;
10
by: Scott Townsend | last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent it to be in specific places (not yet 100% defined). I wanted to create a structure/class with all of the data in it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.