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

inserting elements of array into another one

56
Expand|Select|Wrap|Line Numbers
  1. #include "stdio.h"
  2.  
  3.  
  4. char t[16] = {0x8F,0x78, 0xC6, 0x12, 0xBA,0x98, 0xA2,0x16, 0x8F,0x78, 0xC6, 0x12, 0xBA, 0x98, 0xA2, 0x16} ;
  5. char w[44];
  6.  
  7. void main(){
  8.  
  9.     for(int i=0 ; i<4 ; i++){
  10.  
  11.         w[i] = (t[4*i], t[4*i+1], t[4*i+2], t[4*i+3]);
  12.         printf("%0x\t",w[i]);
  13.  
  14.     }
  15. }
i want to put some elements of the t array into the w array but when i do this, it prints only t[4*i+3]
i want the output to be something like this
w[0] = t[4*i], t[4*i+1], t[4*i+2], t[4*i+3] = 0x8F,0x78, 0xC6, 0x12
w[1] = 0xBA,0x98, 0xA2,0x16
and so on
Jan 16 '14 #1
2 1258
Banfa
9,065 Expert Mod 8TB
w[0] is a char, it can not have the value 0x8F,0x78, 0xC6, 0x12 which is 4 chars.

Did you mean to make w an array of arrays char w[11][4] ?

I suspect that (t[4*i], t[4*i+1], t[4*i+2], t[4*i+3]) is not doing what you think. The parenthesis do very little in this expression and I do not think you understand what the comma operator does.

The comma operator evaluates both it's expressions and returns the value of the right hand expression. it is left-to-right associative.

So in (t[4*i], t[4*i+1], t[4*i+2], t[4*i+3]) it evaluates t[4*i], t[4*i+1] returning the value of t[4*i+1], then it evaluates t[4*i+1], t[4*i+2] returning t[4*i+2] finally it evaluates t[4*i+2], t[4*i+3] returning t[4*i+3] for the whole expression hence w[0] has this value.

The comma operator is really not very useful and in 20+ years programming I have yet to use it in anger ever.

Additionally you have got the data type of t wrong. a char can hold a value in the range -128 - 127 but you are assigning it values in the range 0 - 255 you should probably be using unsigned char.
Jan 16 '14 #2
donbock
2,426 Expert 2GB
main should return int, not void.

To expand on what @Banfa said about char versus unsigned char, there are 3 different types:
  • char is used for characters, but should not be used for numbers. True, characters are encoded as integer values, but both the range of integral values supported and the character encoding vary between compilers. Don't ever fool yourself into thinking you know what they are.
  • signed char is used for integers between -128 and +127.
  • unsigned char is used for integers between 0 and 255.
Whenever a signed char or unsigned char variable appears on the right-hand side of an equals sign, its value is promoted to int. In many cases you might as well have used int from the start.
Jan 16 '14 #3

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

Similar topics

6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
1
by: Bijoy Naick | last post by:
I need to be able issue a query, then insert each record in the reader into another table. But, when I issue the myCommand.ExecuteNonQuery call, I get an error telling me that a reader is...
2
by: Adam Hartshorne | last post by:
Hi All, I was wondering if somebody could tell me if there is an efficient way to do the following. Say I have a list(or vector) A and a list B, I want to remove any elements in B, that are also...
15
by: laredotornado | last post by:
Hi, Using PHP 4.4.4, I have this array $my_arr = array("a", "b", "c", "d"); What is the easiest way to insert the element "|" between the elements of the array so that the result is ...
2
by: JuiceyBananas | last post by:
I have two tables a and b with columns in them, say a.test1 and b.test2, which are both ntext. I've done a lot of reading online about UPDATETEXT and WRITETEXT but I don't have enough experience to...
5
by: angel120 | last post by:
So my second hw is to solve the 8 queens problem using a 1D array. The professor gave us a code that outputs the following (labeled as q): 0 2 5 7 6 3 1 4 The spaces 0-7 refer to the column #,...
10
by: Debajit Adhikary | last post by:
I have two lists: a = b = What I'd like to do is append all of the elements of b at the end of a, so that a looks like: a =
2
by: Simon Brooke | last post by:
I have a document type which I'm developing and working with, which is currently defined in a DTD, mainly because I still haven't really learned to use schemas. In this document type I need to...
5
by: billelev | last post by:
I have a large array of data (1000 x 40 x 3) that I am inserting into a database table. It is incredibly slow, and so I was wondering if there is a quicker way of inserting array data into a table....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.