Debugging C Program for Outputting Array Elements

Click For Summary

Discussion Overview

The discussion revolves around debugging a C program that outputs elements of an array. Participants focus on understanding compiler warnings related to format specifiers in the printf function, specifically when printing memory addresses and values from an integer array.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • The original poster reports a compilation error related to using the format specifier '%d' for printing pointers, which expects an 'int' but receives an 'int *'.
  • One participant suggests using '%p' for printing addresses or casting the address to an 'int', noting that older compilers may have allowed this but it is problematic in 64-bit systems.
  • Another participant emphasizes that compiler warnings are less serious than errors but can indicate significant issues if ignored.
  • A later reply argues that warnings can sometimes be more serious than errors, as they may lead to executing buggy code if overlooked.
  • One participant agrees that while warnings can vary in severity, they can still allow for compiled code that produces output for further debugging.

Areas of Agreement / Disagreement

Participants generally agree on the importance of addressing compiler warnings, but there is some disagreement regarding the severity of warnings compared to errors and how they should be handled.

Contextual Notes

Participants discuss the implications of using outdated practices in programming, particularly in relation to format specifiers and compiler behavior, without resolving the broader implications of these practices.

rclakmal
Messages
76
Reaction score
0
C programme ?

Homework Statement


/* Program-8.4 */
#include <stdio.h>
int main()
{
int marks[5]= {89, 45, 73, 98, 39};
printf("%d\n", marks); //memory addres pointed by pointer
printf("%d\n", &marks[0]); //memory address of 1st element
printf("%d\n", *marks); //value pointed by pointer
printf("%d\n", marks[0]); //value of 1st array element
return 0;


i can't run these source code >it give an error saying
laka.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
laka.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
line 6 and 7 refers to the lines where marks and & marks[0] are present .
please point out the error >great help !



Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org


The compiler is correct, format ‘%d’ DOES expects type ‘int’, but argument 2 has type ‘int *’
You can either use '%p' or cast the address to an int, some older compilers did allow you to treat an address as an int but it causes problems with 64bit and isn't really allowed.
 


thanks u so much mgb_phys .It was a great help .this programme is in my textbook .So i was amazed as it didn't compile .But now i understand as it was written sometime ago ,those days compilers had allowed to do that .
 


And keep in mind that the compiler issued warnings, which are not as serious as errors.
 


rclakmal said:
printf("%d\n", marks); //memory addres pointed by pointer
printf("%d\n", &marks[0]); //memory address of 1st element
I believe the correct format string for this is %p.

Mark44 said:
And keep in mind that the compiler issued warnings, which are not as serious as errors.
That's arguable. Warnings are quite often more serious than errors: if you made a mistake and get an error, you can't compile your program; however if you made a mistake and ignore the warning, your buggy code actually gets executed.
 


I agree in general about the warnings vs. errors, but there are different categories of warnings, some of which can be safely ignored and some of which can't. At least with a warning, you get compiled code that can produce output for further analysis and debugging.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 21 ·
Replies
21
Views
4K