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

Requiring & operator for one member of struct, but not for another.

Markus
6,050 Expert 4TB
Can you explain why line 31 does not require the address-of operator, while 33 does? I don't quite understand it.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. struct computer {
  4.     float cost;
  5.     int   year;
  6.     int   cpu_speed;
  7.     char  cpu_type[16];
  8. };
  9.  
  10. typedef struct computer SC;
  11.  
  12. void DataReceive(SC *ptr_s);
  13.  
  14. int main(int argc, char** argv)
  15. {
  16.     SC model;
  17.  
  18.     DataReceive(&model);
  19.     printf("Here is what you entered: \n");
  20.     //printf("Year: %d\n", model.year);
  21.     //printf("Cost: %6.2f\n", model.cost);
  22.     printf("CPU Type: %s\n", model.cpu_type);
  23.     printf("CPU Speed: %d MHz\n", model.cpu_speed);
  24.  
  25.     return 0;
  26. }
  27.  
  28. void DataReceive(SC *ptr_s) 
  29. {
  30.     printf("CPU Type: ");
  31.         scanf("%s", ptr_s->cpu_type);
  32.     printf("CPU Speed: ");
  33.         scanf("%d", &ptr_s->cpu_speed);
  34. }
  35.  
Thanks,
Mark.
Oct 12 '09 #1
4 1650
donbock
2,426 Expert 2GB
@Markus
From scanf documentation:
  • Conversion specifier s: the corresponding argument shall be a pointer to the initial character of an array large enough to accept the sequence and a terminating null character, which will be added automatically.
  • Conversion specifier d: the corresponding argument shall be a pointer to integer.
The argument for the %s conversion specifier is a char array. An array specifier is almost always equivalent to a pointer to the first element of the array; hence ptr_s->cpu_type is the same as &ptr_s->cpu_type[0] so it is a pointer to char as expected by scanf.

The argument for the %d conversion specifier is the address of an int; hence it is a pointer to int as expected by scanf.

By the way, you might prefer the following to preclude overflowing the cpu_type array:
Expand|Select|Wrap|Line Numbers
  1.     scanf("%15s", ptr_s->cpu_type);
Oct 12 '09 #2
Markus
6,050 Expert 4TB
Thank you very much, Donbock. That helps a whole bunch.

Quick q - what happens if the char array does overflow? I have experimented, but I didn't understand the output: stack smashing detected.

Thanks,
Mark.
Oct 12 '09 #3
Banfa
9,065 Expert Mod 8TB
If you write outside the bounds of the array you get undefined behaviour.

Undefined behaviour is bad because anything can happen. The worst thing that can happen is that the program works as expected right up until the moment it is critical that it works and then it doesn't work in some catastrophic way.

If you are lucky undefined behaviour produces some result that you can immediately identify as wrong. However it is often hard to diagnose the exact cause of undefined behaviour when it happens.

stack smashing specifically refers to your compiler detecting that you have written to a portion of the stack that was not being used for the array. This is bad because the stack contains many critical pieces of information like the return address to jump to once the function has finished. Overwriting this would be a critical error (I hope you can see).
Oct 12 '09 #4
donbock
2,426 Expert 2GB
Suppose the operator responds to the 'CPU Type" prompt by entering 63 characters plus newline. The scanf function will write the 63 characters plus terminating null (64 characters in all) to the array. However, the size of the array is only 16 characters. This means that the next 48 bytes of memory are unintentionally corrupted.

Those 48 bytes might be other program variables; they might be cpu register values saved on the stack; they might be a function return address saved on the stack; they might be nonexistent memory; they might be memory-mapped I/O devices, or they might be executable code within your program.

The consequences of corrupting those 48 bytes are truly unpredictable. That's why the C Standard calls this "undefined behavior".

When I said earlier that these bytes are unintentionally corrupted; I meant from the programmer's point-of-view. It might indeed have been the operator's intention to overflow your buffer. This is one of the techniques used by black hats to take over a computer.

You may have heard that Standard C function gets is deprecated. That's because unlike scanf where you can specify the maximum field length, gets provides no way for the program to protect itself from an operator who deliberately or inadvertently overflows the input buffer.
Oct 12 '09 #5

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

Similar topics

5
by: Avinash | last post by:
Hi, Why Overloaded operator cannot be a friend ? Thanking You. Avinash
7
by: Mikhail N. Kupchik | last post by:
Hi All. I have a question regarding Herb Sutter's idiom of implementation of operator= via nonthrowing swap() member function, to guarantee strict exception safety. The idea of the idiom is...
14
by: indigodfw | last post by:
Greetings from India I would like to know the rationale on allowing structs to be assigned (using = operator) and not allowing equality operator ( == operator) on them. The compiler when it...
7
by: Eric Lilja | last post by:
>From a book, I know the following is true for the comparison operators: An overloaded operator that is a class member is only considered when the operator is used with a *left* operand that is an...
5
by: Gianni Mariani | last post by:
I'm hoping someone can tell me why using member address of works and why using the dot operator does not in the code below. The code below uses the template function resolution mechanism to...
8
by: pauldepstein | last post by:
The following code was written by a colleague -- to preserve confidentiality, the name of the class is changed: HisClass operator+ (const HisClass & h1, const HisClass & h2) { // some code here}...
7
by: MWimmer | last post by:
Dear members of this group, recently I came across a problem with repsect to operator=() and inheritance. Consider the following code snippet: ...
34
by: mdh | last post by:
Hi All, Just when I thought things were going to get easy! Structs. I **thought** I had copied the examples pretty closely, but am getting a number of errors. The code:
2
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng ...
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:
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.