![]() |
| | #1 |
| Registered User Join Date: Apr 2009
Posts: 6
| Code: #include <stdio.h>
#include <stdlib.h>
int size = 0;
struct a{
int x;
};
void add(struct a *as){
as = realloc(as, size++ * sizeof(struct a));
as[size - 1].x = size;
printf("%d\n", as[size -1].x);
}
int main(){
struct a *as = NULL;
add(as);
printf("%d\n", as[size - 1].x);
return 0;
}
|
| paolor is offline | |
| | #2 |
| Registered User Join Date: Feb 2009 Location: &CProgramming
Posts: 12
| many errors.... i)after u made ur data type named 'a',why r u using struct always.... remember its like others ..int,float....why complicate it.... ii)how will main() know that u changed something in add()...if u keep add() as void here's mine.. Code: #include <stdio.h>
#include <stdlib.h>
int size=0;
typedef struct
{
int x;
}a;
a *add(a *as)
{
as = (a*)malloc(++size * sizeof(a));
as[size - 1].x = size;
printf("%d\n", as[size -1].x);
return as;
}
int main()
{
a *as;
as =add(as);
printf("%d\n", as[size-1].x);
return 0;
}
|
| Tanuj_Tanmay is offline | |
| | #3 |
| Registered User Join Date: Feb 2009
Posts: 278
| the add(as); in the original post is results in the same thing as the same line in your code. In the original, he passes a pointer to the structure, so any changes to as within the function are reflected in as in main. So as = add(as); is redundant. |
| Bladactania is offline | |
| | #4 | ||
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Quote:
Quote:
Your program is wrong for what they're trying to do though. They're trying to have the ability to reallocate the array bigger and bigger each time. The actual problem is that the size is being incremented AFTER it's being allocated. So what's the problem? What's zero * something? Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | ||
| quzah is offline | |
| | #5 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Quote:
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #6 |
| Registered User Join Date: Apr 2009
Posts: 6
| I dont want that the add function returns a struct pointer. This works, but .... How can i save a int in x and to have the same value int the main? |
| paolor is offline | |
| | #7 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| you could pass pointer to pointer
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #8 |
| Registered User Join Date: Apr 2009
Posts: 6
| pointre to pointer? Hwo? |
| paolor is offline | |
| | #9 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| pass in add(&as), make the function take add(a **as), perhaps? -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #10 |
| Registered User Join Date: Apr 2009
Posts: 6
| Thanks matsp, but how? Could you edit my code with your idea. Please |
| paolor is offline | |
| | #11 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| Why? you cannot edit your code yourself?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #12 |
| Registered User Join Date: Apr 2009
Posts: 6
| Yes I can do it, but I cannot what to do. Please, could you post the right code. Last edited by paolor; 04-23-2009 at 11:51 AM. |
| paolor is offline | |
| | #13 |
| Protocol Test Engineer Join Date: Sep 2005 Location: fseek(UK)
Posts: 1,316
| >but I cannot what to do. haha i like that. lol Well, people here won't write the code for you here. We can just hint you but not write the code or give the code. Here is a peice of hint Code: void add(struct a **as){ <-- This is how the function prototype should look like
Code: struct a *as = NULL; add(&as); -ssharish
__________________ Life is like riding a bicycle. To keep your balance you must keep moving - Einstein |
| ssharish2005 is offline | |
| | #14 |
| Registered User Join Date: Apr 2009
Posts: 6
| thank you |
| paolor is offline | |
![]() |
| Tags |
| array, segmentation fault, struct |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dynamic array of structures containing yet another dynamic array of structures | innqubus | C Programming | 2 | 07-11-2008 07:39 AM |
| question about multidimensional arrays | richdb | C Programming | 22 | 02-26-2006 09:51 AM |
| Binary search of an array of pointers to structs using pointer arithmetic | mgimbl | C Programming | 41 | 07-04-2004 03:23 PM |
| array of structs initialization - PLZ help... | Vanya | C++ Programming | 2 | 12-11-2002 08:10 PM |
| Pointer to Array of Structs | Unregistered | C Programming | 2 | 03-06-2002 08:34 AM |