Thread: declaring chars: char file[<nonconstant>] [1024];

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    declaring chars: char file[<nonconstant>] [1024];

    is there any way to create an array of characters like this:

    Code:
    int numfiles;
    cin >> numfiles;
    char files[numfiles] [1024];
    it says i need a constant variable up there, and i tried (const int)numfiles... lol, but of course, that didn't work

    so, do i have to do a typedef or whatever that command is?

    (sorry if this has already been asked)

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    that compiles w/out errors with mingw

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    well, this is in visual studio

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    ok, nevermind, i found out a way to do it (new, delete)

  5. #5
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109
    here:
    #include <iostream.h>
    #include <stdio.h>


    void main()
    {
    char **t;
    int size;

    printf("size:");
    cin>>size;

    t=new char *[size];

    for(int a=0; a<size; a++)
    t[a]=new char[1024];

    t[0]="Josh";

    printf("%s\n",t[0]);
    t[1][2]=60;
    printf("%d\n",t[1][2]);

    }
    int a; don't make a program without it.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    oook, here's what i got:

    Code:
    	int numfiles;
    	cin >> numfiles;
    	char *files;
    	files=new char[numfiles*256];
    and that creates it, to view stuff, i just do this:

    Code:
    	char filename[256];
    	strcpy(filename,"blah.txt");
    	for(int c=0;c<256;c++)
    	{
    		files[a*256+c]=filename[c];
    		if(filename[c]=='\0')
    			break;
    	}
    and just for "good housekeeping"

    Code:
    	if(files)
    		delete [] files;
    Last edited by jverkoey; 03-30-2003 at 01:47 AM.

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    and just for "good housekeeping"
    Code:
    if(files)
    	delete files;
    That should read "bad housekeeping". Either that, or the code should read:
    Code:
    delete[] files;
    deleting a NULL pointer has no effect, so checking for NULL before you call delete is inefficient.

  8. #8
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109
    ya and he forgot []
    int a; don't make a program without it.

  9. #9
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109
    is there a small way to do it like that with two diynamics?
    int a; don't make a program without it.

  10. #10
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    sorry, i forgot to put the [] in there......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM