Thread: use of pointers with char arrays

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    4

    use of pointers with char arrays

    im creating a program that reads in a csv fields and breaks it up into records and files using structures. my problem is that when i use the field constructor function with a char array it doesn't work, it prints out nothing:
    Code:
    RcrdArray[v].fieldArray[1] = Field_Constructor(0, 54, Field);
    but when i use it like this it works, it prints out "any word":
    Code:
    RcrdArray[v].fieldArray[0] = Field_Constructor(0,54, "any word");
    this is what the structures look like:
    Code:
    struct Rcrd Rcrd_Constructor(){
    	struct Rcrd record;
    	record.fieldArray;
    	record.rcrdDelimit = '\n';
    	return record;
    }
    struct Field Field_Constructor(int idx, int size, char *field){
    	struct Field fld;
    	fld.fldidx = idx;
    	fld.fldchrptr = field;
    	fld.fldsize = size;
    	fld.fldDelimit = ',';
    	return fld;
    }
    //this is the way i declared the array of pointers to Rcrd structs
    struct Rcrd *RcrdArray
    //this is the way i declared the field array
    char Field[40] = {0};
    I've spent a long time messing with this and changing things and i cant figure it out. I'm not very familiar with pointers and i have a feeling that it has something to do with that. Any help is appreciated. thanks.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    What does struct Field look like?

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct Rcrd Rcrd_Constructor(){
    	struct Rcrd record;
    	record.fieldArray;
    	record.rcrdDelimit = '\n';
    	return record;
    }
    You realize this does absolutely nothing, right?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    4
    Quote Originally Posted by swoopy
    What does struct Field look like?
    Code:
    struct Field{
    	int fldidx;
    	char *fldchrptr;
    	int fldsize;
    	char fldDelimit;
    };
    
    struct Rcrd{
    	struct Field fieldArray[8];
    	char rcrdDelimit;
    	struct rcrd *NextRcrd;
    };

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    4
    Quote Originally Posted by quzah
    You realize this does absolutely nothing, right?

    Quzah.
    no, i didnt. can i just leave it out or should i change it?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > fld.fldchrptr = field;
    Unless field never changes in whoever called Field_Constructor, it might be better to make a copy.

    > char *fldchrptr;
    You would have to change this to an array instead of a pointer:
    char fldchrptr[80];
    or whatever size it needs to be.

    > fld.fldchrptr = field;
    Then use strcpy() here:
    strcpy( fld.fldchrptr, field );

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    4
    thank you! it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM