c - Segmentation fault at passing array of integers to function -
i have pasted code here, @ statement *stack_ptr->s3++ = element;
segmentation fault occurring. can please let me know should change eliminate error?
typedef int stack_elmnt_int; typedef struct { stack_elmnt *s1; stack_elmnt_int *s3; int length; int top; } stack; int push_stack_int(stack *stack_ptr, stack_elmnt_int element) { int i=0; if (stack_full(stack_ptr)) printf("\nstack full."); else{ *stack_ptr->s3++ = element; } } void main() { int arr[128], arr_num = sizeof(arr)/sizeof(int); //after input user arr //partial code pasted here for(i=0;i<arr_num ;i++) push_stack_int(&stack_1,arr[i]); }
statement
(*stack_ptr->s3++ = element;)
segmentation fault occuring,
probably don't allocate memory s3
.
*stack_ptr->s3++ = element; ^ | assigning garbage location
can please let me know should change eliminate error?
in main() function after declaration of stack_1
allocate memory s3
( should allocate memory s1
), like:
stack_1.s3 = calloc(max_number_of_element, sizeof( stack_elmnt_int));
additionally, should not increment s3
pointer. (you loss starting address) like:
stack_ptr->s3[top++] = element;
Comments
Post a Comment