C mixing with strings

C mixing with strings

 

   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

 

4 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

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.

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?

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.

Melden Sie sich an, um einen Kommentar zu hinterlassen.