Hey guys,
The lastest episode in the saga of my wrestle to get to grips with C revolves around my inability to handle arrays.
I've been tasked with recreating the Soundex code. This involves several stages - removing vowels from a set of characters, removing double letters, and then translating it into a numeric code.
However, that all sounds pretty daunting, so to get to grips...
For a start you can't call
Code:
a[26] ='\0';
when you intialised the array with
Code:
char a[26];
As you are accessing the 27th element (it's numbered from 0-25)
So you'll be getting memory corruption issues right there.
Same with
Code:
for (i=0,i<=20,i++)
You're trying to access the 21st element.
And surely you don't want
Code:...
Dave,
Thanks for pointing out my newbish mistakes! And taking the time to explain why they are so.
I've modified the length of a [] to 27 characters (I didn't understand before that was how they were numberd) to prevent that fault. I've done a similar thing with the i variable by reducing the continuation parameter to i<=19.
I've also changed the scanf and printf to reflect the correct...
Firstly, please use the
Code:
code
tag for code as it preserves the tabbing!
The first problem I can see is you are doing:
if (B[i]=a[0])
You want the comparason operator '==' not '=', the asignment operator. But then you are assigning B[i]=0; This will not remove the vowel, but terminate the string. So "test" will change to "t\0st" which is "t".
You are also ...
you cant initialise an array like that. do this as an example instead
char a[5] = "ABCDE";
or
char a[5] = {'A','B','C','D','E');
also using single letters as variable names is not recommended and a practice I would advise you to get out of!
Is 'devowel' a function? It may just be the formatting of the text but if it is then the syntax is all wrong
You can initialise an array that way, it far from great, but it works. I guess technically you are not initialising the array at all and leaving it up to the whims of the compiler but setting all the values very soon after. However, my next suggestion was going to be to rework how that array work completely and it isn't the ops biggest issue
I was curious as to what is going on with the ...
my point is, there is a declaration (char a[]) some code and then another declaration (char b[]) - this is not allowed in C and likely to be one of the causes of the errors
Hey everyone,
I've taken in your comments and spent some more time researching. I've made a few changes and updated my op with the correct coding. I understand however there are a few fundamental flaws with the way I'm coding, but for now that's how I understand it.
Quote:
You want the comparason operator '==' not '=', the asignment operator. But then you are ...
Quote:
Originally Posted by BullBoyShoes
my point is, there is a declaration (char a[]) some code and then another declaration (char b[]) - this is not allowed in C and likely to be one of the causes of the errors
Declaration of char b[] is commented out.
UPDATE2: I missed one thing you changed. I see B is ...
Quote: Originally Posted by peteruk Does that mean my shift function still needs changing though? Yes, you must make sure that all array indexing stays inside its bounds. C does no checks to detect (or stop) this and array out-of-bounds problems will cause major headaches later. EDIT: However, there is always an exception to the rules. Strings in C are null terminated. So the string "test" is stored using 5 bytes,...
For a start you can't call Code: a[26] ='\0'; when you intialised the array with Code: char a[26]; As you are accessing the 27th element (it's numbered from 0-25) So you'll be getting memory corruption issues right there. Same with Code: for (i=0,i<=20,i++) You're trying to access the 21st element. And surely you don't want Code: scanf ("%f",B); As thats reading in floats? Surely you...
You could use a While loop which stops when the character is null then you will not need to do the count and will notoverflow the inputted string. Code: int something( char * s ) { while( *s++ ) { //do your business } return //an int } But as thats a bit of a pain to read you can always just do the strlen inside the function rather than having to pass it in and make it a bit nicer to use.
Thread profile page for "Noobish C Fail - 'Permission Denied' - recreating Soundex code" on http://www.overclockers.co.uk.
This report page is a snippet summary view from a single thread "Noobish C Fail - 'Permission Denied' - recreating Soundex code", located on the Message Board at http://www.overclockers.co.uk.
This thread profile page shows the thread statistics for: Total Authors, Total Thread Posts, and Thread Activity