13 Replies - 3064 Views - Last Post: 19 March 2011 - 01:56 PM Rate Topic: -----

#1 Rhianna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-March 11

copy a string into pointer array to char

Posted 19 March 2011 - 11:55 AM

Ya'll must be about tired from me not getting a grip on this pointer array thing but here goes...


 
#include <stdio.h>
#include <string.h>
#define LEN_STR 100
#define MAX_ROWS 10000

int main(void)
{
    int i = 0;
    FILE *ofp;
    FILE *ifp;


    char str [LEN_STR+1];
    char *records[MAX_ROWS];

    ifp = fopen("FishFood.txt","r");
    if(ofp == NULL)
    {
        printf("Error in opening the file");
    }
    else
    {
        ofp = fopen("Fishy2.txt","w"); // ignore this for now
        if(ifp == NULL)
        {
            printf("Error in opening the file");
        }
        else
        {

                while(!feof(ifp)&& i < MAX_ROWS){

                  fgets(str, sizeof(str), ifp);
                  strcpy(records, str);  //copy string into pointer to string char's
                  printf("%s", records); // print off current record
                  *(records+i);  // advance the pointer address
                  i++;
                }

                fclose(ifp);


           printf("%s", records); // only prints the last string on the file list
                                  //, which makes me think the pointer address does not advance
                                 // and each string just gets replaced by the newer one
        }                        // I'm expecting to return the first string copied into records
   }


    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: copy a string into pointer array to char

#2 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:09 PM

A simple way is to copy every element from the string to the *char array.Something like this:
while(string[i]!='\0'){   //  \0 is the mark that tells you that your string is ended 
     *char[i]=string[i];
     i++;
}


This post has been edited by Patrunjel: 19 March 2011 - 12:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 Rhianna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-March 11

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:21 PM

View PostPatrunjel, on 19 March 2011 - 12:09 PM, said:

A simple way is to copy every element from the string to the *char array.Something like this:
while(string[i]!='\0'){   //  \0 is the mark that tells you that your string is ended 
     *char[i]=string[i];
     i++;
}



hum, dunno if this makes a difference or not, but one line of strings reads like this.
TetraFin Goldfish Flakes - 2.20 Pounds,24.00,24.00
I want to put each line into a memory address for further manipulation afterwards
Was This Post Helpful? 0
  • +
  • -

#4 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:27 PM

you can't put a string in a char.In strings all the letters are in a single object (wrapped), in char you can't put more than one character in a variable.I mean, char[10] can store only one letter, for example.

This post has been edited by Patrunjel: 19 March 2011 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:30 PM

You need use the subscript operator in your strcpy. Each index of records can hold a string, but you keep copying into the first index each time.

 *(records+i);  // advance the pointer address



isn't doing what you think its doing. If you were to use that in your strcpy call then my above statement would be invalid. It's only advancing the pointer temporarily.

In addition to all of the above you still need to allocate space to hold the strings you want to copy.

Quote

I mean, char[10] can store only one letter, for example.


??? You can store ten letters in there, and a collection of letters is a "string".
Was This Post Helpful? 1
  • +
  • -

#6 Patrunjel  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 17
  • View blog
  • Posts: 298
  • Joined: 28-October 10

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:31 PM

yes, in all the array you can store 10 letters, but in only one element (the 10th in my example) you can store only one.Gezus, my english sucks :))
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is online

  • member icon


Reputation: 4158
  • View blog
  • Posts: 12,952
  • Joined: 25-December 09

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:34 PM

First your variable *records[MAX_ROWS] should probably be records[MAX_ROWS][LEN_STR] so it will hold MAX_ROWS C-strings of LEN_STR length. Otherwise you will need to assign memory to this variable with malloc.

Your printf on line 43 will only print one C-string. If you want it to print more than one you will need to put it into a loop.

Also when I compile your code I get the following warnings/errors:

Quote

main.c|24|warning: C++ style comments are not allowed in ISO C90|
main.c|24|warning: (this will be reported only once per input file)|
main.c||In function ‘main’:|
main.c|35|warning: passing argument 1 of ‘strcpy’ from incompatible pointer type|
/usr/include/string.h|127|note: expected ‘char * __restrict__’ but argument is of type ‘char **’|
main.c|36|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|37|warning: statement with no effect|
main.c|44|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|18|warning: ‘ofp’ is used uninitialized in this function|
||=== Build finished: 0 errors, 7 warnings ===|


Jim
Was This Post Helpful? 1
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:35 PM

View PostPatrunjel, on 19 March 2011 - 01:31 PM, said:

yes, in all the array you can store 10 letters, but in only one element (the 10th in my example) you can store only one.Gezus, my english sucks :))


Not in this case, he hasn't allocated any space for the strings. Currently all we have is an array of ten char pointers. We have no idea what each can hold because we haven't defined it yet.
Was This Post Helpful? 0
  • +
  • -

#9 Rhianna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-March 11

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:42 PM

View PostPatrunjel, on 19 March 2011 - 12:27 PM, said:

you can't put a string in a char.In strings all the letters are in a single object (wrapped), in char you can't put more than one character in a variable.I mean, char[10] can store only one letter, for example.

what? char[10] is a char string with a place for "huhsaywhat"
a char can only store one letter or character!
you just made a typo right? ;-)

thanks for trying to help out though.

View PostKYA, on 19 March 2011 - 12:30 PM, said:

You need use the subscript operator in your strcpy. Each index of records can hold a string, but you keep copying into the first index each time.

 *(records+i);  // advance the pointer address



isn't doing what you think its doing. If you were to use that in your strcpy call then my above statement would be invalid. It's only advancing the pointer temporarily.

In addition to all of the above you still need to allocate space to hold the strings you want to copy.

with malloc , or otherwise it would segfault right KYA ?
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3108
  • View blog
  • Posts: 19,146
  • Joined: 14-September 07

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:47 PM

That is correct.
Was This Post Helpful? 0
  • +
  • -

#11 Rhianna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-March 11

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:50 PM

View Postjimblumberg, on 19 March 2011 - 12:34 PM, said:

First your variable *records[MAX_ROWS] should probably be records[MAX_ROWS][LEN_STR] so it will hold MAX_ROWS C-strings of LEN_STR length. Otherwise you will need to assign memory to this variable with malloc.

Your printf on line 43 will only print one C-string. If you want it to print more than one you will need to put it into a loop.

Also when I compile your code I get the following warnings/errors:

Quote

main.c|24|warning: C++ style comments are not allowed in ISO C90|
main.c|24|warning: (this will be reported only once per input file)|
main.c||In function ‘main’:|
main.c|35|warning: passing argument 1 of ‘strcpy’ from incompatible pointer type|
/usr/include/string.h|127|note: expected ‘char * __restrict__’ but argument is of type ‘char **’|
main.c|36|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|37|warning: statement with no effect|
main.c|44|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|18|warning: ‘ofp’ is used uninitialized in this function|
||=== Build finished: 0 errors, 7 warnings ===|


Jim

thanks Jim and all the guys that looked into my problem!
this site is great because of you, thanks again.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4158
  • View blog
  • Posts: 12,952
  • Joined: 25-December 09

Re: copy a string into pointer array to char

Posted 19 March 2011 - 12:53 PM

You're welcome. If you don't get any of the warnings you need to change your compiler settings to enable warnings. Just using errors alone is not enough to produce good code.


Jim
Was This Post Helpful? 0
  • +
  • -

#13 Rhianna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-March 11

Re: copy a string into pointer array to char

Posted 19 March 2011 - 01:05 PM

View Postjimblumberg, on 19 March 2011 - 12:34 PM, said:

First your variable *records[MAX_ROWS] should probably be records[MAX_ROWS][LEN_STR] so it will hold MAX_ROWS C-strings of LEN_STR length. Otherwise you will need to assign memory to this variable with malloc.

Your printf on line 43 will only print one C-string. If you want it to print more than one you will need to put it into a loop.

Also when I compile your code I get the following warnings/errors:

Quote

main.c|24|warning: C++ style comments are not allowed in ISO C90|
main.c|24|warning: (this will be reported only once per input file)|
main.c||In function ‘main’:|
main.c|35|warning: passing argument 1 of ‘strcpy’ from incompatible pointer type|
/usr/include/string.h|127|note: expected ‘char * __restrict__’ but argument is of type ‘char **’|
main.c|36|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|37|warning: statement with no effect|
main.c|44|warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char **’|
main.c|18|warning: ‘ofp’ is used uninitialized in this function|
||=== Build finished: 0 errors, 7 warnings ===|


Jim

I just put that in there as a test.
Thanks Jim, checking my compiler settings as we speak.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5884
  • View blog
  • Posts: 12,769
  • Joined: 16-October 07

Re: copy a string into pointer array to char

Posted 19 March 2011 - 01:56 PM

The first sign of a program in crisis is trying to put the whole bloody thing in main. Use some functions, they're free:
#include <stdio.h>
#include <string.h>

#define LEN_STR 100
#define MAX_ROWS 10000

char *copyString(const char *str) {
	char *newStr;
	/* your code here
	just strcpy is not enough
	you must malloc as well
	*/
	return newStr;
}

/* returns number read */
int readRecords(FILE *ifp, char *records[]) {
	int count = 0;
	while(!feof(ifp)&& count < MAX_ROWS){
		char str[LEN_STR+1];
		fgets(str, sizeof(str), ifp);
		printf("read: %s", str);
		records[count++] = copyString(str);
	}
	return count;
}

void printRecords(char *records[], int size) {
	/* your code here
	you must loop from 0..size-1 and show each record
	*/
}

/* I can't see what you're doing with ofp yet, but we'll pretend */
void processOpenFiles(FILE *ifp, FILE *ofp) {
	char *records[MAX_ROWS];
	int count = readRecords(ifp, records);
	printRecords(records, count);
	/* perhaps do the same thing as printRecords, but print to file? */
}

int processFile(const char *inFileName, const char *outFileName) {
	FILE *ifp = fopen(inFileName,"r");
	if(ifp == NULL) {
		printf("Error in opening the file");
	} else {
		FILE *ofp = fopen(outFileName,"w"); // ignore this for now
		if(ofp == NULL) {
			printf("Error in opening the file");
			fclose(ifp);
		} else {
			processOpenFiles(ifp, ofp);
			fclose(ifp);
			fclose(ofp);
			return 0;
		}
	}
	return 1; // oops
}
	
	
int main() {
	return processFile("FishFood.txt", "Fishy2.txt");
}



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1