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

returning matrix

56
Expand|Select|Wrap|Line Numbers
  1. unsigned char* intitial_Add_Round_Key(unsigned char plaintxt[16], unsigned char key[16]){
  2.     unsigned char initial_output[16];
  3.     for(int i=0 ; i<16 ; i++){
  4.         initial_output[i] = plaintxt[i]^key[i];
  5.  
  6.     }
  7.     return initial_output;
  8.  
  9. }
  10. void main(){
  11.     unsigned char plaintxt[16]= {0xA9, 0x58, 0x19, 0xB1,0x8C, 0x96, 0xF2, 0x63,0x45 ,0x78, 0xDE, 0xA5, 0xB7, 0x55, 0x23 , 0x12} ;
  12.     unsigned char key[16]= {0x8F,0x78, 0xC6, 0x12, 0xBA,0x98, 0xA2,0x16, 0x8F,0x78, 0xC6, 0x12, 0xBA, 0x98, 0xA2, 0x16} ;
  13.     unsigned char initial_output[16];
  14.     unsigned char* t;
  15.     t = intitial_Add_Round_Key(plaintxt,key);
  16.     for(int h = 0 ;h<16;h++){
  17.       printf("%u",t[h]);
  18.       printf("\t");
  19.     }
  20.     //substitute_bytes(initial_output);
  21.     scanf("%d");
  22.  
  23. }
i am trying to return the matrix in a variable called "t"
but it prints another number (not the matrix)
Jan 15 '14 #1

✓ answered by weaknessforcats

You can't return a matrix from a function. C/C++ lets you return a type or a pointer to a type.

A matrix is not a type. Instead it is a collection of a type. In the case of your matrix, what is being returned is the address of initial_output[0].

But there are worse problems. initial_output[16] is a local variable which is destroyed after the function completes but the returned address lets you use deleted memory, which will cause your program to crash.

You might read: http://bytes.com/topic/c/insights/77...rrays-revealed

1 1361
weaknessforcats
9,208 Expert Mod 8TB
You can't return a matrix from a function. C/C++ lets you return a type or a pointer to a type.

A matrix is not a type. Instead it is a collection of a type. In the case of your matrix, what is being returned is the address of initial_output[0].

But there are worse problems. initial_output[16] is a local variable which is destroyed after the function completes but the returned address lets you use deleted memory, which will cause your program to crash.

You might read: http://bytes.com/topic/c/insights/77...rrays-revealed
Jan 15 '14 #2

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

Similar topics

3
by: Dave | last post by:
Hi there: i am doing a matrix class for practice. everything is ok except the operator. Basically, i want to achieve the same way as the normal double's, such as: m = 4.0; m = m + m;...
1
by: Dawn Minnis | last post by:
Hey guys - this code when called with parameters: driver.o n n 12 12 12 12 12 12 2.6 3.2 is kicking back a segmentation fault. I've read the rest of the postings but am still confused. Can...
8
by: shan | last post by:
How to return an two dimensional array in user defined function to main function. I think it is posible by using pointers but don't know how to code.or is there any method without using...
10
by: andrew browning | last post by:
i have overlaoded all of my arithmetic operators but all are functioning as multiplication. below is a sample of the addition operator: Rational operator + (const Rational& r1, const Rational&...
8
by: lovecreatesbeauty | last post by:
I write a function to rotate a matrix by 90 degrees clockwise, this function works on a matrix of specific size, for example, it rotates a 4*4 matrix of integers in the following code. The function...
13
by: Gernot Frisch | last post by:
Which method is the fastest/best: std::vector<intfoo1() { std::vector<intv; ... reutun v; } std::vector<int>& foo2()
9
by: josh | last post by:
Hi, I'm converting (for learning purpose) )a program from Java to C++ and I've a doubt: In Java every argument (variable and reference types) is passed and returned in functions by-value so if I...
4
by: hefty1985 | last post by:
Dear all, I hope you can help. Is any one aware of a function that would return the number of Rows and Columns of a 2-D array in C ( Matrix)? Or, how to go about writting a function with the...
6
by: thorsten.schilling | last post by:
Hello everybody, the question is, what is the "golden way" or the best way, if I have a memberfunction in a class, which should return a new instance of an object. For example some class Foo...
4
by: Encrypted | last post by:
i hav coded a matrix class and its member functions (many of them are overloaded ones)...now in a function..for eg. function of matrix addition.. after the addition i want to return the local object...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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
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,...

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.