ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
On Cygwin platform, on compiling C language program below, get the error, stated in comments, inside the program.
Code:
#include <stdio.h>
int main()
{
int *pnum;
char *pch;
printf(" Size of integer pointer = %d\n\n", sizeof(pnum));
printf("size of char pointer = %d", sizeof(pch));
int x=10;
int *ptr;
ptr = &x;
printf("\n \n ptr is: %i", x);
int x1[6] = {1,2,3,4,5,6};
ptr = x1;
int i = 0;
while (*ptr && i<=5)
printf("\nitem # %d ", i, "is :%d", *(ptr+i++));
//Doubt: Only i is printed, while the value there is not printed on cygwin?
//first tried with a similar expression:
//printf("\n item # %d ", i, "is :%d", ptr[i++]);
//that should yield the same result as above, as ptr[i++] is the same as *(ptr+i++)
}
The format specifier %d expects an integer argument, but "is :%d" is a string argument. To fix this, you need to separate the two format specifiers and provide two corresponding arguments. One for %d and another for the string.
The order of the arguments is incorrect. The value to be printed should come after the format specifiers.
Here's the corrected printf statement:
printf("\nitem # %d is :%d", i, *(ptr+i++));
This will properly print the item number (i) and the corresponding value (*(ptr+i++)).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.