Thread: Strings :0

  1. #1
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28

    Strings :0

    Hiii im new

    I was playing around with C and there are a few things i'd like to be cleared up

    1.) So i can't get the length of an array of characters without keeping track of it myself?
    2.) I was trying to write an encryption function - because you know.... funfunfun XDD - And i got a bit confused.

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    char Shift(char Achar,int Shifts)
    {
        bool Shiftleft = 
        Shifts <= 0;
        if (Shiftleft)
         Shifts=sqrt(pow(Shifts,2)); //i wonder if -1*val would be faster
        int Lowerbound = 33;
        int Remainder;
        int Upperbound = 126;
        Remainder = 
    Shifts%(Upperbound-Lowerbound);
        if (Shiftleft)
        {
            if (Remainder>
            (int)Achar-Lowerbound)
            {
                return (char)(Upperbound+(((int)Achar-Lowerbound)-Remainder)+1);
            }
            else return (char)((int)Achar-Remainder);
            
        }
        else 
        {
            if (Remainder>
            Upperbound-(int)Achar)
             {
                return (char)(Lowerbound+(Remainder-(Upperbound-(int)Achar))-1);
            }
            else return (char)((int)Achar+Remainder);
        }
    }
    
    int Encrypt(char *Astring,char *Buffer,int *NewLength)
    {
        return 1;
    }
    int main(void)
    {
        char Test[0];
        int Length;
        Encrypt("Hello",Test,&Length);
        printf("Length : %i\n",Length)
        return 0;
    }
    I know it's not complete but the point was basically this. In delphi I could return a string very easily eg : copy("ab",1,1) would return 'a'
    Now knowing the following i attempted to do the same in C.

    On my phone i can't really see the terminal crash but it didn't work properly. So i read up on pointers because well they seemed to be the solution.

    Encrypt takes three paramaters.
    A string literal.
    A pointer to an array of characters which will basically be the encrypted
    String literal.
    and finally a pointer of type int that i will use to store the length of the encrypted string.

    Oh and 3) How effective is my shift function.


    While fondling some pointers i just made a small program to memorise so I wouldn't forget how it works.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int Changec(char *Arr)
    {
        Arr[1]='G';
        return 1;
    }
    void Changei(int *Pointer)
    {
        *Pointer=3;
    }
    int main(void)
    {
        char Test[]="Hello";
        int Temp;
        Changec(Test);
        printf("%c%c%c\n",Test[0],Test[1],Test[2]);
        Changei(&Temp);
        printf("%i",Temp);
        return 0;
    }
    This is just really frustrating. Working with languages like delphi and c# just feels so much simpler but i must admit i love the way C feels ^.^ I'm doing this as preperation for university so any and all advise would be greayly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RagingGrim
    1.) So i can't get the length of an array of characters without keeping track of it myself?
    If your array of characters is merely an array of characters, then yes. This holds true for all arrays. However, if your array of characters contains a null terminated string, then you can get the length of the string by counting characters until the first null character. For your convenience, the strlen function already does this. Likewise, for any other array, you can designate your own special value to function as the "end". However, it is usually better to explicitly keep track of the number of elements in use and/or the size of the array so as to avoid accessing the array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2014
    Location
    Centurion, Gauteng, South Africa
    Posts
    28
    Thanks!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    A few other comments:

    Quote Originally Posted by RagingGrim
    Code:
    Shifts=sqrt(pow(Shifts,2)); //i wonder if -1*val would be faster
    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.

    In this case, a person reading your nested calls of two functions has to recognise that calling pow with the second argument of 2 squares the number, upon which calling sqrt obtains the absolute value of the original number. Since we already know that Shifts is non-positive, just multiplying by -1 would be more explicit, simpler, avoid unnecessary nesting of function calls, and all in all would be more readable. Simpler yet, you could also write:
    Code:
    Shifts = -Shifts
    There is no need to keep casting Achar to int: the type promotion rules in C will take care of that.

    The size of a fixed length array must be greater than 0. That you got away with:
    Code:
    char Test[0];
    is probably due to a compiler's language extension that was meant for a different purpose (and also perhaps because Encrypt is just a stub).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-16-2012, 06:08 AM
  2. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  3. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  4. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  5. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM

Tags for this Thread