C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-22-2009, 12:33 PM   #1
Registered User
 
Join Date: Apr 2009
Posts: 6
Question Array of structs

Hi! I have problems with an array of structs. This is an example of my problem.

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;
}
Segmetation fault is the error. Why?
paolor is offline   Reply With Quote
Old 04-22-2009, 12:59 PM   #2
Registered User
 
Tanuj_Tanmay's Avatar
 
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;
}
I don't use realloc....malloc rocks!!
Tanuj_Tanmay is offline   Reply With Quote
Old 04-22-2009, 01:16 PM   #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   Reply With Quote
Old 04-22-2009, 02:54 PM   #4
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Quote:
Originally Posted by Tanuj_Tanmay View Post
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....
No it's not. This isn't C++. You cannot omit the word struct if you have not created a new type with typedef.
Quote:
Originally Posted by Tanuj_Tanmay View Post
ii)how will main() know that u changed something in add()...if u keep add() as void
Well, considering they're changing size, they could compare the value before and after the call. However, unless they use a pointer to a pointer, their pointer won't be keeping track of the newly allocated memory. Returning it as you have shown will get around that limitation as well.

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   Reply With Quote
Old 04-22-2009, 02:57 PM   #5
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Quote:
Originally Posted by Bladactania View Post
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.
That's wrong. You cannot change what the pointer points at, unless you actually have the address of that pointer. A pointer will let you change the value of what is pointed at. Not what is pointed at.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-23-2009, 03:45 AM   #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   Reply With Quote
Old 04-23-2009, 04:59 AM   #7
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by paolor View Post
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?
you could pass pointer to pointer
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 04-23-2009, 09:17 AM   #8
Registered User
 
Join Date: Apr 2009
Posts: 6
pointre to pointer? Hwo?
paolor is offline   Reply With Quote
Old 04-23-2009, 09:42 AM   #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   Reply With Quote
Old 04-23-2009, 10:00 AM   #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   Reply With Quote
Old 04-23-2009, 10:46 AM   #11
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
Quote:
Originally Posted by paolor View Post
Thanks matsp, but how? Could you edit my code with your idea. Please
Why? you cannot edit your code yourself?
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 04-23-2009, 11:21 AM   #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   Reply With Quote
Old 04-23-2009, 04:28 PM   #13
Protocol Test Engineer
 
ssharish2005's Avatar
 
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
and in the main

Code:
struct a *as = NULL;
add(&as);
As you can see your sending the address of the pointer 'as' to the function. But since you are passing the pointer address and function 'add' should refereing to pointer to a pointer struct a.

-ssharish
__________________
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
ssharish2005 is offline   Reply With Quote
Old 04-24-2009, 05:38 AM   #14
Registered User
 
Join Date: Apr 2009
Posts: 6
thank you
paolor is offline   Reply With Quote
Reply

Tags
array, segmentation fault, struct

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:11 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22