Debugging C Program for Outputting Array Elements

In summary: With an error, you don't even get that. So in this specific case, I would argue that the warning is not as serious as the error. It's still an issue that needs to be addressed, but it doesn't completely halt the development process.
  • #1
rclakmal
76
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
  • #2


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.
 
  • #3


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 .
 
  • #4


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


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.
 
  • #6


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.
 

Related to Debugging C Program for Outputting Array Elements

1. How do I debug my C program for outputting array elements?

To debug your C program for outputting array elements, you can use a debugger tool like GDB. Set breakpoints in your code and use the step-by-step execution feature to track the values of your array elements as the program runs.

2. Why am I getting unexpected output when trying to display my array elements?

There could be several reasons for unexpected output when displaying array elements. Check that you are using the correct index values and that your loop conditions are properly set. Also, make sure that your array is properly initialized and that you are not accessing out-of-bounds memory.

3. How can I print the contents of my array in a specific format?

To print the contents of your array in a specific format, you can use the printf() function. Use formatting specifiers, such as %d for integers or %f for floats, to specify the type of data you want to print. You can also use modifiers like %10d to specify the field width.

4. Can I use a debugger to modify the values of my array elements?

Yes, you can use a debugger to modify the values of your array elements while the program is running. This can be useful for testing different scenarios and checking the output at different stages of the program execution.

5. How do I fix a segmentation fault error when trying to display my array elements?

A segmentation fault error when trying to display array elements is usually caused by accessing memory that is out of bounds or has not been allocated. Make sure you are using the correct index values and that your array is properly declared and initialized. You can also use a debugger to track the memory addresses being accessed and identify the cause of the error.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
762
  • Engineering and Comp Sci Homework Help
Replies
4
Views
940
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
679
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
Back
Top