Ex11 problems at breaking

I’m having trouble with this assignment:
" Accidentally set name[3] = ‘A’; so that there’s no terminator "
I tried many things but it always prints ‘Zed’:
Here is the code:


Here are the results:
Ex11_results

Where do you call name[3]?
I see you only print name[0], name[1], name[2]
Also show me the part of the code where you set name[3] to “A”

1 Like

Yes I see I mixed all of the breaking methods together.
Instead I’ll try to explain how I understand the methods. This time without mixing them.

I’m using a VM and don’t know how to copy from it to the PC. That’s why I’m uploading pictures. uploading the code will just make a mess. So I’m referring to the code as written at Ex11 at the book. Hope that’s ok.

Here is the print output of the code before breaking it.
EX11_regular

In the book it says how to break it:

Get rid of the initializers that set up name.
What I did is to delete lines 23-27 where you setup name[0] =Z etc. (//setup the names…)
printed:
Ex11_break_del

Accidentally set name[3] = ‘A’; so that there’s no terminator.
Changed line 27 " name[3] = ‘\0’ " to " name[3] = ‘A’ "
printed:
Ex11_break_A

Set the initializer to {‘a’,‘a’,‘a’,‘a’} so that there are too many ’a’ characters and no space for the ‘\0’ terminator.

   Here I've change line 6 from " char name[4] = { 'a' } " to "char name[4] = { 'a', 'a', 'a', 'a' } "
   prints:

ex11_break_3
doesn’t seem broken…

Thanks for all the help
much appreciated

1 Like

Hey, can you browse the forum from within you VM?

1 Like

Yes I can :slight_smile:

For some reason a part of the code is in a window. Don’t know what to do with it.

I didn’t add the output of these changes cause it would make this unreadable. So the question is only if the changes I did are the changes needed to be done according to this assignment:

#1 Get rid of the initializers that set up name .
#2 Accidentally set name[3] = ‘A’; so that there’s no terminator .
#3 Set the initializer to {‘a’,‘a’,‘a’,‘a’} so that there are too many ’a’ characters and no space for the ‘\0’ terminator .

Here is the original code with 3 comments, 1 for each breaking method:

#include <stdio.h>

int main(int argc, char *argv[])
{
	int numbers[4] = { 0 };
	**char name[4] = { 'a' };  #3 This I've change to { 'a', 'a', 'a', 'a'}**

	//first print them out raw
	printf("numbers: %d %d %d %d\n",
		numbers[0], numbers[1], numbers[2], numbers[3]);

	printf("name each: %c %c %c %c\n",
		name[0], name[1], name[2], name[3]);

	printf("name: %s\n", name);	

	//setup the numbers
	numbers[0] = 1;
	numbers[1] = 2;
	numbers[2] = 3;
	numbers[3] = 4;	

**(#1 Get rid of the initializers that set up name. So I deleted the code below in bold text )**
	**//setup the name**  
	**name[0] = 'Z';**
      **name[1] = 'e';**
      **name[2] = 'd';**
      **name[3] = '\0'; Here is #2 where i've changed to name[3] = 'A';**

	// then print them out initialized
	printf("numbers: %d %d %d %d\n",
		numbers[0], numbers[1], numbers[2], numbers[3]);

	printf("name each: %c %c %c %c\n",
		name[0], name[1], name[2], name[3]);

	// print the name like a string
	printf("name: %s\n", name);

	//another way to use name
	char *another = "Zed";

	printf("another: %s\n", another);

	printf("another each: %c %c %c %c\n",
		another[0], another[1], another[2], another[3]);

	return 0;
}

Hey, so you need to put this around your code:

[code]

[/code]

I did that for you, but if you forget next time you can click the pencil icon to edit your post and add it back.

1 Like