Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Guys, I created two files. I know that a static variable has just file scope. So, here I am trying to take a global pointer and pointing to that static variable and want to use that static variable's value through pointer in my other file. But I am unable to do it. Please correct me!

File 1.c:
Objective-C
#include <stdio.h>
static int name;
int *p=&name;
int main()
{
    extern name;
    name = 20;
    extern *p;
    printf("add of p is %p and value odf p is %d\n",p,*p);
    return 0;
}


File 2.c:
Objective-C
#include <stdio.h>
void strlength(void)
{
    extern int *p;
    printf("value of name from 1.c is: %d\n",*p);
}


Output:
add of p is 0x804a028 and value of p is 20
Posted
Updated 24-Jul-14 5:19am
v3

Try this
C++
// file 1.c
#include <stdlib.h>
#include <stdio.h>

static int name;
extern int *p=&name;
int main()
{
    extern void strlength();
    name = 20;
    printf("add of p is %p and value odf p is %d\n",p,*p);
    strlength();
    
    getchar();
    return 0;
}
</stdio.h></stdlib.h>


C++
// file 2.c
#include <stdlib.h>
#include <stdio.h>

void strlength(void)
{
    extern int *p;
    printf("value of name from 1.c is: %d\n",*p);
}
</stdio.h></stdlib.h>
   
This way it would work
C
#include <stdio.h>
static int name;
int *p=&name;
void strlength(void);
int main()
{
  name = 20;
  printf("add of p is %p and value odf p is %d\n",p,*p);

  strlength();

  return 0;
}

However that's a good example of bad code.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900