10 Replies - 386 Views - Last Post: 07 December 2017 - 10:35 PM Rate Topic: -----

#1 Joshieboy2007  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 140
  • Joined: 26-March 14

C Array issue

Posted 07 December 2017 - 01:32 AM

I have an array i have called char name[4];

And i'm calling in a text file which has lines of different internal codes to run to create different sizes of blocks.

So when it calls in Create i have it as
char named = *args[1];



where args is the name, But it also calls it more than once

and

name[req] = &named; 



To store it for later use

When i run the code if it was for example create A ,B ,C ,D

it would take the last value and make all of them D.

If i remove the & and * when I try to access it, it takes some line from within the program.

name is created as:

char * name[15];




I am unsure of what exactly happening.

This post has been edited by Joshieboy2007: 07 December 2017 - 01:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C Array issue

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1406
  • View blog
  • Posts: 4,450
  • Joined: 20-March 10

Re: C Array issue

Posted 07 December 2017 - 05:00 AM

use strcpy to copy one c string to another.

This post has been edited by snoopy11: 07 December 2017 - 05:01 AM
Reason for edit:: double post again

Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 5358
  • View blog
  • Posts: 16,731
  • Joined: 25-December 09

Re: C Array issue

Posted 07 December 2017 - 05:16 AM

You also need to understand the difference between a single character and a C-string (an array of char).

char named = *args[1];

Here named is a single character and args is a pointer to what is probably an array of char (a C-string).

Jim
Was This Post Helpful? 0
  • +
  • -

#4 Joshieboy2007  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 140
  • Joined: 26-March 14

Re: C Array issue

Posted 07 December 2017 - 11:30 AM

okay, i adjusted my code where:

char named[15];



and using strcpy
strcpy(named,args[1];



than putting it into my array to keep track of nameds

name[req] = named;



But it it still doing the same thing.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 5358
  • View blog
  • Posts: 16,731
  • Joined: 25-December 09

Re: C Array issue

Posted 07 December 2017 - 11:35 AM

You need to provide a small complete program that illustrates your problems, then ask specific questions on the code posted. The snippets you're now posting do not provide enough content are are requiring guess work in order to provide answers, and I for one am a horrible guesser.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1406
  • View blog
  • Posts: 4,450
  • Joined: 20-March 10

Re: C Array issue

Posted 07 December 2017 - 11:35 AM

you cant use

name[req] = named;




its
strcpy(name, named);


we already went over that.....
Was This Post Helpful? 0
  • +
  • -

#7 Joshieboy2007  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 140
  • Joined: 26-March 14

Re: C Array issue

Posted 07 December 2017 - 11:49 AM

okay here is the code i'm trying to work on

int main(int argc, char ** argv)
{
	char * readmepath; // read mename
	char linebuf[MAX_BUFFER];                  // line buffer
	char cwdbuf[MAX_BUFFER];                   // cwd buffer
	char * args[MAX_ARGS];                     // pointers to arg strings
	char ** arg;                               // working pointer thru 
	

	if(!getcwd(cwdbuf, sizeof(cwdbuf))) 
	{  
	syserrmsg("getcwd",NULL);
	cwdbuf[0] = 0;
	}	
	strcat(cwdbuf,"/" README);      // make full readme pathname
	readmepath = strdup(cwdbuf);
	
	//Get Fit Type
	printf("%s\n",argv[3]);
	
	//Get N Sizei
	double N = atoi(argv[2]);
	//printf("N = %f\n",N);
	long Total =  (long)pow(2,N);	
	//printf("Tgfdsg = %f\n",Total);
	//Get fileName	
	char buf[MAX_BUFFER];
	FILE *file;
	size_t nread;
	int i = 0;
	int part[15];
	char * name[15];
	int block = 0;

	file = fopen(argv[3], "r");
	
	int mem[10];
	//Find number of processes
	int numOfProc = 1;
	int procNum =0;
	char * ProcName;
 	mem[0] = Total;
	name[0] = "FREE";
	if(!strcmp(argv[1], "BESTFIT"))
	{
		printf("******FIRSTFIT*******\n");
	if (file != NULL ) {
		while ( fgets (linebuf, MAX_BUFFER, file ) != NULL )
		{
            arg = args;
            *arg++ = strtok(linebuf,SEPARATORS);   // tokenize input
            while ((*arg++ = strtok(NULL,SEPARATORS)));
			//printf("%s\n", args[1]);	
	    	if(!strcmp(args[1], "AVAILABLE"))
			{
			printf("WE ARE IN AVALIABLE!\n");
		        int ava = 0;
			int full = 0;
			printf("Avaliable:");
			while((ava <= procNum) && (mem[ava] >= 0))
			{
			printf("(%d,%d)" ,ava,mem[ava]);
		        	
		 	if(mem[ava] !=0)
			{
				full++;
			}
			ava = ava + 1;  
			}
			printf("\n");
			if(Total <= 0)
			{
				printf("All blocks full\n");
				printf("Total: %d\n)",mem[ava]);
			}
			}
                if(!strcmp(args[0], "REQUEST"))
			{
			printf("WE ARE IN REQUEST!\n");  
			 int req = 0;
			 int size = atoi(args[2]);
			 while(req <=  procNum)
			{
			 if(mem[req] >= size && mem[req] != 0)
			 {
				 printf("ENTERING INTO REQUEST VALUE: %d",req);
			 int temp = mem[req];
			 mem[req] = 0;

			 char named[15];
			 strcpy(named,args[1]);
			 printf("test %s\n",named);
			 mem[req+1] = temp - size;
			 part[req] = size;
		         name[req] = named;
	printf("SIZE and Req: %d %d\n",size,req);
			//strcpy(name,named);
//printf("this is name: %s\n",name[req]);  
			break;
			 }
			req = req + 1;
			
			}
procNum = procNum + 1;
			}
	    	if(!strcmp(args[0], "RELEASE")) 
		{
		printf("WE ARE IN RELEASE! %s\n",args[1]);  
		int req = 0;
		int gSize = 0;
		printf("PROCNUM:  %s , %d\n",args[1],procNum);
		while(req <= procNum)
		{
		if( args[1] == name[req])
		{
		printf("PARTS: %s\n",args[1]);
			gSize = part[req];
		printf("GSize and name: %s , %d\n",name[req],gSize);
			name[req] = '\0';
			part[req] = 0;
			mem[req] = gSize;
		}
		req++;
		}
		}

		//
		//FIND Function
		//

		if(!strcmp(args[0], "FIND"))
		{
		 printf("WE ARE IN FOUND\n");
		int find = 0;
		int found = 0;
		
		while(find <= procNum)
		{
			if(name[find] == args[1])
			{
				printf("(%s,%d)\n",args[1],part[find]);
				found = 1;
			}
			find++;
		}

		if(found == 0)
		{
			printf("No matches found\n");
		}

		}
		
		if(!strcmp(args[0], "#"))
		{
		printf("# Check to see if A was added\n");
		}
		//printf("NAME OUTSIDE: %s\n",name[0]);
		if(!strcmp(args[1], "ASSIGNED")) 
			{

			printf("WE ARE IN ASSIGNED!\n");  
			printf("NAME SHOULD BE!!! %s\n",name[0]);	
		       
			int assi = 0;
			 printf("Procnum: %d\n",procNum);
			printf("Assigned: ");
			 while(procNum > assi )
			 {
				 //printf("MEM and Assi %d ,%d\n",mem[assi],assi);
			if(mem[assi] == 0)
			{
			printf("(%s,%d)",name[assi],part[assi]);
			}
			assi++;
			}
			printf("\n");
			}
		}
	}
    	if (ferror(file)) {
        printf("IN VALID FILE\n");
    	}
    	fclose(file);
	}
	else if(!strcmp(argv[1],"FIRSTFIT"))
	{
	}
	else
	{
	}	

      	return 0;	
}




I believe the error is somewhere in request
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon

Reputation: 5358
  • View blog
  • Posts: 16,731
  • Joined: 25-December 09

Re: C Array issue

Posted 07 December 2017 - 12:04 PM

And what exactly are you attempting to do?

strcat(cwdbuf,"/" README);      // make full readme pathname

You may want to read some documentation for strcat, it requires only two C-strings.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 Joshieboy2007  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 140
  • Joined: 26-March 14

Re: C Array issue

Posted 07 December 2017 - 12:46 PM

I'm essential making a list of some size, and as processes are being requested it will take it out of total create a block of some size and than move the free to the end of the array. Each process has a Name and size. Each process can be released in which it will give back to the total and it will be free memory again.

Note that it says BESTFIT but i'm actually just working on fistfist
Was This Post Helpful? 0
  • +
  • -

#10 Joshieboy2007  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 140
  • Joined: 26-March 14

Re: C Array issue

Posted 07 December 2017 - 06:56 PM

snoopy i tried doing strcpy(name[req],named), an strcpy(name,named); both of which didn't work i kept reciving the Segmentation fault (core dumped) error
Was This Post Helpful? 0
  • +
  • -

#11 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1406
  • View blog
  • Posts: 4,450
  • Joined: 20-March 10

Re: C Array issue

Posted 07 December 2017 - 10:35 PM

The seg fault is happening because you haven't allocated any memory for the operation.

try this

    int N = 10;

    char** name = malloc(15*sizeof(char*));

    for (int i=0; i<N;i++)
    name[i]= malloc (N *sizeof(char));

    char named[15];
    strcpy(named, "someData");

    strcpy(name[0], named);

    printf("some output %s", name[0]);



and for goodness sake buy a decent book on C and read it...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1