Thread: strcpy problem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    3

    strcpy problem

    hi everyone

    I ran into an error when working on this assignment in MS Visual Studio 2008. I need to get a paragraph of text into an array of strings (with each string holding one line) and it must be dynamically allocated.

    when the program runs to the strcpy line (indicated by /*PROBLEM HERE*/ below, I get an access violation error from the strcat.asm library. I couldn't find the bug

    The "1" is used for testing.

    I see this from local variable window of the strcat.asm tab:
    + dst 0x0041573c "1" unsigned char *
    + src 0x0abaf9d4 <Bad Ptr> unsigned char *


    here's the code

    Code:
    #define INPUTFILE "file1.txt"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int main()
    {
    	FILE *fp;
    	int length;
    	char *stringarray;
    	int *charactercount;
    	int *charstring;
    	int lines = 0;
    	int c = 0;
    	int i;
    	char *tempstring;
    	char character;
    
    	if ((fp = fopen(INPUTFILE, "r")) == NULL) 
    	{
    		printf ("Error opening file"); 
    		getchar();
    		exit(1);
    	}
    	fseek(fp,0,SEEK_END); 
    	length=ftell(fp);
    	fseek(fp,0,SEEK_SET);
    
    	/*count number of lines*/
    	charstring = malloc(length);
    	while(fgets(charstring, 63, fp)!=NULL) /*62 is max characters per line*/
    		{lines++;}
    
    	charactercount = calloc(lines, sizeof(int));
    	fseek(fp,0,SEEK_SET);
    
    	for (i = 0; i<lines; i++)
    	{
    		while((character = fgetc(fp))!='\n')
    		{
    			if(character == EOF){ break;}
    			c++;
    		}
    		charactercount[i]= c;
    		c=0;
    	}
    
    	for (i=0;i<lines;i++)
    	{
    	printf ("%d\n", charactercount[i]);
    	}
    	fseek(fp,0,SEEK_SET);
    
    	stringarray = malloc(lines * (sizeof (*stringarray)));
    
    	if (stringarray!= NULL)
    	{
    		for (i=0;i<lines;i++)
    		{
    		tempstring = malloc(charactercount[i]*sizeof(char)+1);
    		fgets(tempstring,63,fp);
    		charstring=stringarray[i];
    		stringarray[i] = malloc ((strlen(tempstring)+1));	
    		charstring=stringarray[i];
    		printf("ZZZ%sZZZ", tempstring);
    
    	         if (stringarray[i])
    		{	
    			strcpy(stringarray[i], "1");/*PROBLEM HERE*/
    			strcpy(stringarray[i], tempstring);/*PROBLEM HERE*/
    		}
    
    		}
    	}
    
    
    
    	return 0;
    }
    Last edited by cplayer1; 04-05-2009 at 08:47 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Check to make sure that the return value of the last malloc() is not NULL.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    i've put it back to the if loop.
    it's not the problem though. same error.

    I think maybe the last allocated memory is corrupted by something before but I couldn't find the bug.

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    stringarray[i] is of type char, strcpy's first argument is of type char*. You probably want to declare stringarray as char** and think about why.

    Have a look at the following code: http://www.0xe3.com/src/file/file.c
    (and make it less stupid by using fgets() in file_read_line())

    Greets,
    Philip
    Last edited by Snafuist; 04-05-2009 at 08:13 PM.
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    3
    oh i see. stringarray is a pointer to an array of pointers, so it needs two asterisks, right?

    btw, it worked. thank you so much!

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cplayer1 View Post
    oh i see. stringarray is a pointer to an array of pointers, so it needs two asterisks, right?
    Yes. On the other hand, sometimes I like to think of it like this:
    T* * p;
    Red = Type the pointer points to
    Green = Tells us it's a pointer
    Dark Orange = Name of pointer
    So it's a pointer to T*.
    (Just remember that the pointer itself has a type too, which is T**.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's up with this strcpy?
    By fanoliv in forum C Programming
    Replies: 7
    Last Post: 06-19-2006, 05:24 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM