| | |
Dynamic Array Help
![]() |
•
•
Join Date: Apr 2005
Posts: 31
Reputation:
Solved Threads: 0
Hi,
I can't figure out what is wrong with the code after trying to fix it for the last six hours i decided to give, please i need help =( big time
I'm trying to make a dynamic array of structures then printing the output of the elements but I haven't successed in either storing the elements of the structure in the array nor printing it
I can't figure out what is wrong with the code after trying to fix it for the last six hours i decided to give, please i need help =( big time
I'm trying to make a dynamic array of structures then printing the output of the elements but I haven't successed in either storing the elements of the structure in the array nor printing it
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 128 typedef char String[MAX]; typedef struct { String name; float s; } ST; int main() { int size, Index; ST *p, *tmpa, *tmp; ST *dArray; //create block //check return value of malloc tmpa= (ST *) malloc (1*sizeof(ST)); if(tmpa==NULL) { printf("Error\n"); exit(1); } else {p=tmpa;} size=-1; printf("Enter information (\"exit\") to exit\n"); printf("What is the name :"); scanf("%s",(*p).name); if(strcmp((*p).name,"exit")==0) { exit(0); } while(strcmp((*p).name,"exit")!=0) { printf("What is the size:"); scanf("%f",&(*p).s); size++; (p[size]).name=(*p).name; p[size].s=(*p).s; tmp=(ST *)realloc(p,(size+1)*sizeof(ST)); //check return value from tmp if (tmp==NULL) { printf("Error\n"); exit(1); } else {p=tmp;} printf("Enter information (\"exit\") to exit\n"); printf("What is the name :"); scanf("%s",(*p).name); }//end of while loop printf("The following new data:\n"); for (Index = 0; Index <= size; Index++) { printf("%s has size %.2f \n",p[Index].name,p[Index].s); } free(p); return 0; }
![]() |
Similar Threads
- get length of a dynamic array (C++)
- Sorting 2D Dynamic array of integers , Snake Style (C)
- works for static need help to make it dynamic (C++)
- dynamic array of structures problem (C++)
Other Threads in the C Forum
- Previous Thread: Question about strcmp
- Next Thread: plz give me ur comments on my project named Networks Analyser
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





