0

I am trying to get the nth character of a string. I am currently using this:

char ch = Word[n];

However, when I build it, it gives me the following error:

error: subscripted value is neither array nor pointer nor vector

I am very confused about why I have this error!

My Whole code is:

#include <stdio.h>
#include <string.h>

#define alpha[] [a b c d e f g h i j k m n o p q r s t u v w x y z]

hash(Word){
     char hashed;
    int t, length;
    length = strlen(Word);
    for (t; length; ++t) {
         char ch = Word[t];
         if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
         {
            int position alpha[ch];
            ch += 9;
            strncat(hashed, &ch, 1);
         }
         else if(ch >= '0' && ch <= '9')
         {
             ch*=2;
             char letter alpha[ch];
             strncat(hashed, &ch, 1);
         }
         else
         { 
             strncat(hashed, &ch, 1);
         }
         printf("%s/n", hashed);
     }
     return hashed;
}

main(){
     printf("Please enter your string to hash: ")
     scanf("%s", WordToHash)
     char* HashedWord
     HashedWord = hash(WordToHash)
     printf("/nYour hashed word is %s", HashedWord)
}

My compiler is the basic one that is pre-installed when you install code::blocks through the AUR in manjaro.

By the way, I am a newbie to C (I only started yesterday)!

Thanks in advance!

James Ashwood
  • 469
  • 6
  • 13
  • I may have other errors in my code. If you spot one I would love to know about it! Thanks – James Ashwood Mar 01 '20 at 11:34
  • 4
    If you only started learning C yesterday, I recommend slowing down, getting a good book, and learning from the basics. There are errors in about half the lines here. Some examples are missing type names, missing semicolons, and whatever that macro is supposed to be. – interjay Mar 01 '20 at 11:36
  • @KamilCuk, string – James Ashwood Mar 01 '20 at 11:37
  • 1
    Sorry, but you can't make up or guess programming, I'm sorry, it just doesn't work that way. You have to learn all the rules one by one and follow them. Stackoverflow is a bad site for newcomers - it's a site for specific programming problems and how to solve them. There is a [definitive C Book guide and list](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list), but nowadays you can find many online C courses. Don't start big - start small. – KamilCuk Mar 01 '20 at 11:46
  • Ok @KamilCuk, I already have a book however I got a bit off track. Thanks for the advice. – James Ashwood Mar 01 '20 at 11:48
  • @interjay Yes, I should – James Ashwood Mar 20 '20 at 19:02

1 Answers1

1

I don't know what kind of C this is, but sure doesn't look like standard C. C is strong-typed language. You have to declare the type of a variable before using it. Also, functions need to have return types. In your case, you should replace

hash(Word){

with

char* hash(char* Word){

I also see some other strange things in your code that shouldn't work/compile in C. My advice is to master the basics first and then get back to this example.

conectionist
  • 2,694
  • 6
  • 28
  • 50