am working with pointers in function A and need them to be accessible for function B
the function B seems to not have the same values
plz help
22 2604 RRick 463
Recognized Expert Contributor
I'm not sure of your situation. Are you passing pointers from A to B? B to A? A and B? Each situation has it own hazards to watch for.
A better description and a code snippet would help.
Generally, if two functions need to share a lot of data, the best solution is to make them a C++ class.
If you are using C, you could also create a structure to host a bunch of parameters, therefore simplifying parameter passing. - struct myStruct {
-
int* p1;
-
}
-
-
// a very inefficient way to count to 100
-
// we assume p1 is initialized here
-
void func1(myStruct s)
-
{
-
cout << (*s.p1)++;
-
if(*s.p1 == 100) return;
-
else func1(s);
-
}
Now there are complications when writing runtime libraries and changing structure layout, but if you are, you should know it.
You can't share locals between functions in any other way.
However, if you only have two funcs, you may consider merging them into one to reduce memory overhead.
Having the pointers in function A available in function B takes two forms:
1) Function B does not change the value of the pointer in function A.
In this case, just pass the pointer using a pointer argument in function B. This will make a copy of the pointer in function A and that allows function B to use the address inside that pointer.
2) Function B needs to change the value of the pointer in function A
In this case, you pass the address of the pointer in function A to function B using a pointer-to-pointer argument in function B. Any changes made in function B by dereferencing the pointer-to-pointer argument will be made in function A.
donbock 2,426
Recognized Expert Top Contributor
What is the calling-hierarchy relationship between functions A and B? - Does function A call function B directly?
- Are functions A and B called by the same caller?
- Something else?
function A and B both are called from the main
void A(ptr *max, ptr*min);
void B(ptr *max, ptr*min);
need the two pointers to be accessible in the B function for proccessing
thx
I don't get it. Shouldn't this be what you want: - int main(int argc, char* argv[])
-
{
-
ptr* max;
-
ptr* min;
-
-
// TODO: initialize max and min
-
-
A(max, min);
-
B(max, min);
-
}
The above code will make the data pointed by max and min available to both functions so, after A modifies the data, B will have access to the modified data.
Or are you trying to change the pointer's address itself? In that case, you'd have to do this: - A(ptr** myptr)
-
{
-
ptr* newptr;
-
// TODO: initialize newptr
-
*myptr = &newptr;
-
}
-
-
int main(int argc, char* argv[])
-
{
-
ptr someVariable;
-
ptr* changeme = &someVariable;
-
-
A(&changeme);
-
-
// Now changeme will contain the address of newptr from A,
-
// and not the address of someVariable!
-
}
btw my data is two arrays
Passsing a function pointer is just like passing a regular pointer. Your function argument just needs to be a function pointer.
This function: - void A(ptr *max, ptr*min);
can be passed as a pointer to a function with this argument: - void MyFunc(void (*)(ptr *max, ptr*min));
Then you call MyFunc by:
In fact, you can use any function as the argument provided that your function has two ptr* arguments and returns void.
donbock 2,426
Recognized Expert Top Contributor
Does this look like what you're trying to do? - static void A(T *max, T *min);
-
static void B(T *max, T *min);
-
-
int main(void) {
-
T max[N];
-
T min[N];
-
A(max, min);
-
B(max, min);
-
...
-
}
-
-
static void A(T *max, T *min) { ... }
-
static void B(T *max, T *min) { ... }
Where N is the number of elements in the arrays and T is the type of each entry in the arrays. Any changes that A and B make to the max and min arrays will be visible to main.
By the way, it is generally considered a good idea when passing arrays to a function to also pass the array size: - static void A(T *max, T *min, size_t nelem);
-
static void B(T *max, T *min, size_t nelem);
-
-
int main(void) {
-
...
-
A(max, min, N);
-
B(max, min, N);
-
...
-
}
We wouldn't have to guess at what you're trying to do if you gave us more information.
donbock 2,426
Recognized Expert Top Contributor
When passing pointers around you need to be careful that you don't use a pointer after the thing being pointed to ceases to exist. For instance, the following code demonstrates that mistake. When function B is called, p points to freed memory. Something bad will happen. - void A(int *p);
-
void B(int *p);
-
-
int main(void) {
-
int *p;
-
if (<something>) {
-
int a;
-
p = &a;
-
A(p);
-
} else {
-
int b;
-
p = &b;
-
A(p);
-
}
-
B(p);
-
}
This is exactly it !!!
The function B in my case has irrelevant data passed through the two pointers
well here is the whole picture:
ma trying to stay as modular as possible my main looks like this:
main()
{
A();
B();
.....
}
and there is all kind of pointers going forthand back between the functions
the problem is those pointers seems to not existe outside the function where they are treted the first time
plz help
Banfa 9,065
Recognized Expert Moderator Expert @momotaro
No your main does not look like that. This example has NO pointers so your question is not relevant to it. You have several people in this thread who I am sure could provide you the answer you require if you would explain your problem accurately but you persist in not posting proper examples of what you are trying to do. The experts and moderators ability to answer your question is limited by the amount of information you provide them about the actual problem you are having and frankly for most of this thread you have left them guess what the problem you are having might be.
I have been following this thread and TBH if I were the Experts/Moderators here I would be quite frustrated by now.
Please post a proper example of your problem, that is a code snippet cut down the the minimum amount of code required to demonstrate your problem that either compiles or demonstrates the compiler errors you need help with.
Banfa
Adminsitrator
The main: -
#include "common.h"
-
#include "Choix des Armes.h"
-
-
#define MAX_SIZE 10000
-
-
void main()
-
{
-
Monstre Squelettique[MAX_SIZE], Grassouillet[MAX_SIZE];
-
Eppee eppeeArr[MAX_SIZE];
-
int nbrEppee;
-
int *longestMonstre, *largestMonstre, *nbrSquelettique = 0, *nbrGrassouillet = 0;
-
int oneEppee[MAX_SIZE], twoEppee[MAX_SIZE];
-
do
-
{
-
MonstreFill(Squelettique, Grassouillet, nbrSquelettique, nbrGrassouillet);
-
//MaxLongMaxLargMonstre(Squelettique, Grassouillet, longestMonstre, largestMonstre);
-
//nbrEppee = EppeeFill(eppeeArr);
-
//CandidateEppee(oneEppee, twoEppee, longestMonstre, largestMonstre, nbrSquelettique, nbrGrassouillet);
-
test(Squelettique, Grassouillet, nbrSquelettique, nbrGrassouillet);
-
//EppeeElue(monstreArr, eppeeArr);
-
}while(UserSaysYes());
-
}
The two functions: - #include "common.h"
-
#include "Choix des Armes.h"
-
-
-
void MonstreFill(Monstre *Squelettique, Monstre *Grassouillet, int *nbrSquelettique, int *nbrGrassouillet)
-
{
-
int i, j, nbrMonstre, monstreType, monstreTaille;
-
printf("a l interieure de MONSTRE FILL\n");
-
do
-
{
-
puts("Nombre de monstre ?");
-
scanf("%d", &nbrMonstre);
-
-
}while(nbrMonstre < 1 || nbrMonstre > 10000);
-
-
//initialisation
-
-
for(i = 0; i < nbrMonstre; i++)
-
{
-
Squelettique[i].type = -1;
-
Squelettique[i].taille = -1;
-
-
Grassouillet[i].type = -1;
-
Grassouillet[i].taille = -1;
-
}
-
-
-
for(i = 0, j = 0; i < nbrMonstre - j, j < nbrMonstre - i; i++, j++)
-
{
-
do
-
{
-
puts("0->grand monstre, 1->petit monstre? largeur(petit monstre), longueur(grand monstre)?");
-
scanf("%d %d", &monstreType, &monstreTaille);
-
-
/*nbrSquelettique = 0;
-
nbrGrassouillet = 0;*/
-
-
}while(monstreType != 1 && monstreType != 0);
-
-
switch (monstreType)
-
{
-
case 0:
-
Squelettique[i].taille = monstreTaille;
-
Squelettique[i].type = 0;
-
j--;
-
break;
-
case 1:
-
Grassouillet[j].taille = monstreTaille;
-
Grassouillet[j].type = 1;
-
nbrGrassouillet++;
-
i--;
-
break;
-
default:
-
puts("If you are reading this you don't existe !!!");
-
}
-
}
-
nbrSquelettique = i;
-
nbrGrassouillet = j;
-
printf("%d %d", nbrSquelettique, nbrGrassouillet);
-
printf("SORTIE DE MONSTRE FILL\n");
-
}
-
#include "common.h"
-
#include "Choix des Armes.h"
-
-
void test(Monstre *Squelettique, Monstre *Grassouillet, int *nbrSquelettique, int *nbrGrassouillet)
-
{
-
printf("%d %d", nbrSquelettique, nbrGrassouillet); // here wrong results!!!
-
/*int i;
-
-
puts("Squelettique");
-
for(i = 0; i < nbrSquelettique; i++)
-
printf("%d\n", &Squelettique[i].taille);
-
-
puts("Grassouillet");
-
for(i = 0; i < nbrGrassouillet; i++)
-
printf("%d\n", &Grassouillet[i].taille);*/
-
}
and here is my defined types: -
#include "common.h"
-
-
typedef struct eppee{
-
int eppeeID;
-
int largeur;
-
int longueur;
-
} Eppee;
-
-
-
typedef struct monstre{
-
int type,
-
taille;
-
}Monstre;
-
-
-
void MonstreFill(Monstre *, Monstre *, int *, int *); // YES
-
-
int EppeeFill(Eppee *);
-
-
void MaxLargMaxLongMonstre(Monstre *, Monstre *, int *, int *);
-
-
void CandidateEppee(Eppee *, int *, int *, int *, int *);
-
-
void Eppeeelue(int *);
-
-
void test(Monstre *, Monstre *, int *, int *);
Banfa 9,065
Recognized Expert Moderator Expert int main()
Always, every time. void is a extension supported by a few compilers but on any other system it is undefined behaviour. - void MonstreFill(Monstre *Squelettique, Monstre *Grassouillet, int *nbrSquelettique, int *nbrGrassouillet)
-
{
-
<snip>
-
-
switch (monstreType)
-
{
-
case 0:
-
Squelettique[i].taille = monstreTaille;
-
Squelettique[i].type = 0;
-
j--;
-
break;
-
case 1:
-
Grassouillet[j].taille = monstreTaille;
-
Grassouillet[j].type = 1;
-
nbrGrassouillet++;
-
i--;
-
break;
-
default:
-
puts("If you are reading this you don't existe !!!");
-
}
-
}
-
nbrSquelettique = i;
-
nbrGrassouillet = j;
-
}
Everywhere you access nbrSquelettique and nbrGrassouillet you write the local pointer value, you don't use the pointer to access the external variable in the calling function so when it is passed to the next function no data has been changed. You want the syntax
(*nbrGrassouillet)++;
or
*nbrSquelettique = i; - void test(Monstre *Squelettique, Monstre *Grassouillet, int *nbrSquelettique, int *nbrGrassouillet)
-
{
-
/* Same error in this function too */
-
}
it was the first thing Iu wrote but gave me a run time error!!!
don't know y???
this is the error:
Unhandled exception at 0x00f219a5 in Choix des Armes.exe: 0xC0000005: Access violation reading location 0x00000000.
normally this error emergs when you are trying to access a non memory allocated pointer !!!!
using visual studio 2008 pro
@momotaro
Yes, exactly.
In your main routine you did this: - int *longestMonstre, *largestMonstre, *nbrSquelettique = 0, *nbrGrassouillet = 0;
creating 4 pointers to int initialized to zero.
You never assign a valid address to nbrSquelettique or nbrGrssouillet in your code, so *nbrSquelettique is an error - the pointer doesn't point to anything.
In MonstreFill() you do this: - nbrSquelettique = i;
-
nbrGrassouillet = j;
assigning integer values to pointer variables. That's not valid.
It seems to me that what you want to do is this: - int main()
-
{
-
int nbrSquelettique, nbrGrssouillet = 0; // these are int, not int*
-
MonstreFill(blah, blah, &nbrSquelettique, &nbrGrssouillet); // pass the ADDRESSES of your ints
-
// ...
-
}
-
-
void MonstreFill(blah, blah, int* nbr1, int* nbr2)
-
{
-
// ...do stuff
-
*nbr1 = i; // now nbrSquelettique's value has changed
-
*nbr2 = j; // same for nbrGrassouillet
-
}
thank you that is working!!
Banfa 9,065
Recognized Expert Moderator Expert
See I said things would get fixed more quickly if you provided a proper example and explanation :D
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object...
|
by: Abhi |
last post by:
I wrote a function foo(int arr) and its prototype
is declared as foo(int arr); I modify the values of the array in the
function and the values are getting modified in the main array which
is...
|
by: Robert Dailey |
last post by:
Hi,
I noticed in Python all function parameters seem to be passed by
reference. This means that when I modify the value of a variable of a
function, the value of the variable externally from the...
|
by: lisp9000 |
last post by:
I've read that C allows two ways to pass information between
functions:
o Pass by Value
o Pass by Reference
I was talking to some C programmers and they told me there is no such
thing as...
|
by: venkatagmail |
last post by:
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible...
| |
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |