- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am trying to use 'C' with FORTRAN , but first I need to learn some 'C' basics:
In the following code everything works fine except the call to print_c_data, i.e., and don't have any output to the pointer name. Can anyone explain me why?
code:
#include <stdio.h>
#include <string.h>
void print_c_data(int w, int *s, char *name)
{
printf("x=%d\n and y=%d\n and the name is %s\n",w,*s);
}
main()
{
FILE *fp;
int x, *y,z;
char str [] = "Peter",*c;
z=4;
y=&z;
printf("z = %d\n",*y);
x=10;
*y=x;
printf("x value is x e %d\n\n",*y);
c=str;
print_c_data(5,y,c);
printf(" Variable pointer value is %s\n",c);
puts(c);
}
output: z = 4 x value is x e 10 x=5 and y=10 and the name is Variable pointer value is Peter Peter
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You don't pass the name pointer to the printf call in print_c_data. You have `w` and `*s` being passed, but not `name`, despite having a %s specifier for it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
thanks
#include <stdio.h> #include <string.h> void print_c_data(int w, int *s, char *name) { printf("x=%d\n and y=%d\n and the name is %s\n",w,*s,name); } main() { FILE *fp; int x, *y,z,arr[]={1,5,7},*ptr; char str [] = "Peter",*c; z=4; y=&z; printf("z = %d\n",*y); x=10; *y=x; ptr=&arr[0]; printf("x value is x e %d\n\n",*y); c=&str[0]; /* or c=str */ print_c_data(5,y,c); printf(" Variable pointer value is %s\n",c); puts(c); print_c_data(5,y,"Hello World"); }
but now I have another doubt inside print_c_data: the integer pointer s is used in printf accessing its value *s, but to print the content of pointer *name it must be accessed in another manner,i.e., printf(......,name) and not printf(.....,*name) why?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's how that language works. A string is a sequence of characters in memory that is identified by a pointer to its first character (and then terminated by a null character after the end of the string).
`*name` would dereference the pointer - giving the value of a single character.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page