Hi,

I am new to programming and I have been doing ok. I have a subject that is in C and I really need to be able to use effectively use pointers. So far my lectures are like snails with replys.. and I have 2 options..

1. play it safe and leave everything in main...
2. get cracking and learn how to correctly use pointers.

So far I have been getting VERY frustrated. And I am messing with this messing with that.. and my code is getting very messed up.

I know that in C you pass complex data types by reference and then you need to dereference them with a pointer... eg

main()
{
MyFunction(&myOwnStruct);
{

MyFunction(MyStruct *myOwnStruct)
{
....... my function code
}

Now that is all good.

I wrote the below code then decided I better put it into functions. I have posted it so far (as after I tackle the idea behind the pointers, I can do the rest)

My issues are -
1. Passing into a function and then into a sub-function.
2. Passing 2d arrays, this had just mucked me up.
3. Making comparisons between pointers and ints

The code is commented

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

#define NUM_OF_BITS 16
#define BIT_ROW 0
#define DEC_VALUE_FOR_BIT_ROW 1

void GetInput(char *inputBinaryValue, int NUMBER_OF_BITS);
int ValidateInputLength(char *inputBinaryValue, int NUMBER_OF_BITS);
int ValidateInputValues(char *inputBinaryValue, int NUMBER_OF_BITS);
void ConvertArray(char *inputBinaryValue, char *myArray, int NUMBER_OF_BITS);

int main()
{
    // some of these vaiables are redundant when I finish, I will clean it up
    int index = 0, validTestOne = 0, validTestTwo = 0;
    int column = 0;
    int decValueForBit = 1;
    int totalDecimalValue = 0;
    int myArray [2] [NUM_OF_BITS] = {0}; //final array for comparrisons
    char inputBinaryValue[NUM_OF_BITS] = {0}; // initial input read in here
    int arrayLength = 0;
    int NUMBER_OF_BITS = NUM_OF_BITS;
    
    // loop while input is invalid
    do
    {
        GetInput(&inputBinaryValue, NUMBER_OF_BITS);
        // eg 16 chars for 16 bits
        validTestOne = ValidateInputLength(&inputBinaryValue, NUMBER_OF_BITS);
        //make sure input is in binary, 1 or 0
        validTestTwo = ValidateInputValues(&inputBinaryValue, NUMBER_OF_BITS);
    }while((!validTestOne)||(!validTestTwo));
    
    //Add input array into myArray for later calculations in the program
    ConvertArray(&inputBinaryValue, &myArray, NUMBER_OF_BITS);
    
    return (0);
}

// get input from keyboard add into array
void GetInput(char *inputBinaryValue, int NUMBER_OF_BITS)
{
    printf("Please enter the %i bit binary code: ", NUMBER_OF_BITS);
    scanf("%s", &*inputBinaryValue);
    
    return;
}

// check input is the correct length, error message if not valid, return boolean
int ValidateInputLength(char *inputBinaryValue, int NUMBER_OF_BITS)
{
    int arrayLength = 0, valid = 0;
    arrayLength = strlen(*inputBinaryValue);
    
    if ((NUMBER_OF_BITS - arrayLength) >= 0)
    {
        valid = 1;
    }
    else
    {
        printf("Invalid input!\nYour input is larger then the current bits.\n");
        printf("Please limit the length to %i places.\n", NUMBER_OF_BITS);
    }
    printf("");
    
    return(valid);
}

// check input is in binary, error message if not valid, return boolean
int ValidateInputValues(char *inputBinaryValue, int NUMBER_OF_BITS)
{
    int index = 0, valid = 0;
    
    for(index = 0; index < NUMBER_OF_BITS; index++)
    {
        if((*inputBinaryValue[index] !='1')&&(*inputBinaryValue[index] !='0')&&
           (*inputBinaryValue[index] != NULL))
        {
            printf("Invalid input!!!\nYou can only enter 0's or 1's\n"
                   "Try again.....\n");
            break;
        }
        else
        {
            valid = 1;
        }
    }
    
    return(valid);
}

/* This method takes in the user input if shorter then the total bits the
 * function will add zeros to the front of the original input value when
 * adding to myArray for the final calculations. Eg For 8 bits, if the user
 * inputs 11 after this method myArray will have 00000011 input into it's first
 * row.
 *
 * You can not see this as the final functions are currently missing but
 * later on in the 2d array called myArray, the first row of the 2d array will
 * have the binary value and the second row will have the related value for each
 * bit obviously increasing by the power of 2.
 *
 * I have not got this code in there as i have to finish the functions and to
 * do that I need to understand pointers. once these functions are working i can
 * apply the syntax to the other functions and finish
 *
 */
void ConvertArray(char *inputBinaryValue, char *myArray, int NUMBER_OF_BITS)
{
    int idx = 0, index = 0;
    int arrayLength = strlen(inputBinaryValue);
    int locToWriteFrom = (NUMBER_OF_BITS - arrayLength);
    
    /* This FOR loop starts writing the input string in the correct position in
     * myArray for the final calculations (to come). eg in a 16 bit input if the
     * user originally inputs 1000 (length being 4) this function will start
     * writing this in te 12th position of myArray 16 - 4 = 12, 12 being the
     * position to start writing to end up with 0000000000001000 in row 1 of
     * myArray after the function id finished
     */
    for(index = locToWriteFrom; index < NUMBER_OF_BITS; index++)
    {
        if(*inputBinaryValue[idx] == '1')
        {
            *myArray[0][index] = 1;
        }
        
        idx++;
    }
    
    return;
}

HELP?? Please? I really want to keep on working and finishing my work.. so far my lecturer and tutor as pretty slow on the reply.

Recommended Answers

All 19 Replies

>1. Passing into a function and then into a sub-function.
Did you asked Google? Anyways, I did it for you: http://www-ee.eng.hawaii.edu/~dyun/ee160/Book/chap6/section2.1.2.html

>2. Passing 2d arrays, this had just mucked me up.
http://www.physicsforums.com/showthread.php?t=270319 and http://cboard.cprogramming.com/c-programming/97898-passing-2-dimensional-array-function.html

>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks. :)

>1. Passing into a function and then into a sub-function.
Did you asked Google? Anyways, I did it for you: http://www-ee.eng.hawaii.edu/~dyun/ee160/Book/chap6/section2.1.2.html

>2. Passing 2d arrays, this had just mucked me up.
http://www.physicsforums.com/showthread.php?t=270319 and http://cboard.cprogramming.com/c-programming/97898-passing-2-dimensional-array-function.html

>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks. :)

I cannot believe students these days. With all the resources they have at their disposal and we have to put up with "Dear John" posts such as the one posted by the OP.

For crying out loud - YOU GUYS HAVE IT EASY

TO ALL YOU LAZY STUDENTS OUT THERE - use the resources available to you and then post a specific question. It amuses me how these students (so-called) post lines and lines of code and then attempt a subliminal approach to getting their questions answered.

Quite frankly, you can do us all a favour and get out of the CS/IT game altogether.

Just search for some Stanford University lectures on the internet (they're available via YouTube) on "Programming Methodology" and you'll understand my take on the lack of quality in the standard of CS education these days - it's an absolute joke! These courses have been "watered down" so much - and the flow on effect is the standard of questions that are asked on online forums.

PS: To all the "real" students out there (and you know who you are) - I apologise to you right now - please do not view this post as an attack on your levels of hard work and attitude - you are acknowledged and appreciated.

commented: Ahem to that! +36

I cannot believe students these days. With all the resources they have at their disposal and we have to put up with "Dear John" posts such as the one posted by the OP.

etc etc etc

Who died and made you the judge and jury? You are going off half cocked.. and you wouldn't have a clue about my situation so please don't assume that you do.

I have NOT been taught passing of 2d arrays into sub-functions and it is not even required atm in my unit (possibly not at all in this unit), I made the choice to go beyond what I am currently being taught in this unit at Uni and I am trying to learn more, program better and get better grades.

I am NOT some 18yo, (fresh out of) school kid that doesn't know what I want to do with my life, that just did Comp Sci because I didn't really know what else I wanted to do. I am actually 30 and I have have the choice to enroll in uni and am very committed to getting the best possible grades, knowledge and opportunities from this process.

ALSO - I am currently an EXTERNAL STUDENT (and will be until I return home at the end of this semester)... so I learn at times through audio, if I am lucky enough to have it in my unit.. but most of the time it is just a pdf teaching document and a prac document. This is very difficult may I add, especially when this doesn't really work well with my learning style. I commit massive amounts of my time to grinding away at this alone, me and my laptop and searching for the gaps in what I am provided. So if I have tried, tried, tried, read, googled and tried some more and I still cant figure it out, I am going to look for clarification. That way I can keep on learning and apply my knowledge to making better more efficient programs AND that is what I was trying to do here.

My lecturer actually comment that I am very active on the Uni board providing supplement information that aligns with what we are learning (Google "UNSW Richard Buckland" for an example).

So I am doing quite the opposite to what you are assuming. I could just hand the already working program that I wrote without the functions and get my grades, as the prac document doesn't even mention functions, but I am trying to go beyond that and learn on my own initiative.

I was actually up to the 4th prac on the Stanford iphone programing .. and am able to draw the polygons on the iphone, but that is in a OO language, which is very different when stepping back to c.

I feel quite insulted and angry about your comments. I urge that if you haven't got anything positive and constructive to add to my thread the please go assume and preach in someone another thread. Or even better go beyond that and don't go dumping your judgemental and crappy negative energy me (or other people).

Nate

commented: The other rep on this thread was accidentally positive. Trying to remedy that. -5

>1. Passing into a function and then into a sub-function.
Did you asked Google? Anyways, I did it for you: http://www-ee.eng.hawaii.edu/~dyun/ee160/Book/chap6/section2.1.2.html

>2. Passing 2d arrays, this had just mucked me up.
http://www.physicsforums.com/showthread.php?t=270319 and http://cboard.cprogramming.com/c-programming/97898-passing-2-dimensional-array-function.html

>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks. :)

I have seen some of these but it is quite confusing.. and I was posting here for feedback so I could try and work it out on my code and then learn that way.

I have one more for yellowSnow -

yellowSnow, my prac said that the program only had to accept the full binary code in bits... I went above that and wanted to write a function that was about to accept any length of binary <= the # of bits and then catenate the extra 0's to the front for the conversion to decimal (see the comment for the final function). This was not stated in my brief, I was trying to go above and beyond.. and make a more robust program.

Anyway I have wasted enough of my time replying over this.

Nate

>I feel quite insulted and angry about your comments.
Grow thicker skin. Nobody cares how you feel, nobody cares what your situation is, and everyone will take you at face value. If you look like a leech, you'll be treated like a leech.

However, that said I think yellowSnow and siddhant3s to a smaller extent both need to learn that it's not always appropriate to be a jackass. There was really nothing wrong with your question, in my opinon. They overreacted.

>I feel quite insulted and angry about your comments.
Grow thicker skin. Nobody cares how you feel, nobody cares what your situation is, and everyone will take you at face value. If you look like a leech, you'll be treated like a leech.

Bla bla bla

Here we go another one...

My situation does whether you like it or not effect my thread and the context in which my question was asked,.. it is very relevant. As I am very much trying to differentiate myself from students that get on boards looking for people to do their homework.

In reply.. I am free to express my feelings and think I did a pretty good job dealing with the ignorant rant made by iAteYellowSnow. As the first few attempts at the reply I deleted, each time become less aggressive.. so if it boiled down from a verbal backlash to "I feel angry about your comments" then I think that is healthy venting. If you don't care about healthy expression then you can hang out with iAteYellowSnow. I on the other hand will express if I feel angry. If you don't care then please pay more attention my reply to iAteYellowSnow which was I urge that if you haven't got anything positive and constructive to add to my thread the please go assume and preach in someone another thread.

I love on the occasional thread how there are keyboard warriors with big mouths,.. that are rude and whilst sitting behind their keyboard. I dare say the exchange would not be rude if it was in person.

So for future possible posters... please keep the topic on THE TOPIC! Which is passing 2d arrays into functions and sub-functions.

Mucho amor para todo,
Nate

commented: I don't give rep ofte, but you deserve this. -7

If you had read the "bla bla bla" part that you so rudely snipped out to make me look like the villain, I was agreeing with you that you were not in the wrong. If that's what I get for trying to help you, you can just piss off. Jerk.

I was going to help you with your C question, but now I won't. Congratulations, you're alienating even your allies now.

When you learn some diplomacy then I would love you to help me with my C.

But with opening comments like - Grow thicker skin. Nobody cares how you feel, nobody cares what your situation is, and everyone will take you at face value.[/I, I am not really interested in what you have to say.. even if the following paragraph (which I did read about iAteYellowSnow being a "jerk") is in some way supportive.

If that is "helping" then I suggest you go out and actually go outside and practice some social skills. Maybe you can help me with Cs and I can help you out with some social skills. Try reading the book, "How to win friends and influence people" sounds quite appropriate to me.

Aagain do not want to offend you and I am not going to mouth off at you.. but I am also not going to take anyone's shit either.

So if you did mean well.. and even if it came out in a bent, skewed and poorly communicated way, well thank you I do actually appreciate that.

So don't get upset, I actually do care about people's feelings, your included.. and I don't have any real issue with you (not sure if I can extend that to iAteYellowSnow though).. But I am just standing up for myself (and I have not been offensive) and that is never a bad thing.

Now if we can get all this bitching aside.. I would rather learn how to pass and modify 2D arrays from within functions and sub-functions.

xo
Nate

commented: when YOU learn some diplomacy, someone might care to help you. until then, you're just another mouth. -3

>So if you did mean well.. and even if it came out in a bent, skewed
>and poorly communicated way, well thank you I do actually appreciate that.

Too little, too late, and wording it like your misunderstanding is my fault. It's a wonder you have any friends at all. By the way, if you were truly interested in peace and happiness, you should have taken more time to understand my post before mouthing off.

You fail. Plonk.

>So if you did mean well.. and even if it came out in a bent, skewed
>and poorly communicated way, well thank you I do actually appreciate that.

Too little, too late, and wording it like your misunderstanding is my fault. It's a wonder you have any friends at all. By the way, if you were truly interested in peace and happiness, you should have taken more time to understand my post before mouthing off.

You fail. Plonk.

Your a cocky girl huh?

commented: did you have anything meaningful to add? -3

Your a cocky girl huh?

Don't mess with Narue. Look at her avatar, you will get some idea :sweat:

Don't mess with Narue. Look at her avatar, you will get some idea :sweat:

Yea I see the anime avatar. Its cute.

Well I never expected such a reaction to my post #3.

@nateuni
First up, I apologise if that post offended you that much. In hindsight, I perhaps should have let the post sit in draft mode before pressing the ol' Submit button.

In my defense, I had been posting responses to quite a number of posts yesterday on this forum. A majority of these posts were of a similar theme i.e. poorly formatted code, vague questions and postings of "reams" of code (much of which lacked the necessary resources or functions to even get them to compile). But nevertheless I persisted and answered those posts which I thought deserved some sort of reply. Your post was one of the last I answered and the upper bound of my patience had been tested.

In the short time that I have participated on these forums, I have been PM'd quite a number of times by various members with help for problems - this was probably due to my "nice" nature. Initially I responded to their requests and then I realised that I was engaging in a disservice to this forum. I now politely ask the senders of such messages to re-post their queries on the public forum. The reason why I'm ranting on about this is to emphasis that I'm not a "nasty' person. I am passionate about the IT industry and I like to talk IT and mentor others when the opportunity arises. This is one of the reasons why I like to participate in online forums.

Now you refer to me as iAteyellowSnow - but a day prior you responded "awesome ....." to my reply to a different post that you originated. Oh well, thems the breaks ... don't take this the wrong way, but being referred to as a "jackass" by Narue bites just a little more than being referred to as "iAteyellowSnow" by you. Actually I quite like that name - if you're a Frank Zappa fan like me, you'll understand the correlation between my user name and your revised version of it. :)

Now, the primary reasons for my posting of thread #3 are:

1) The title of your thread didn't match the content of your query - with regards to the passing of 2D arrays. In addition your "God damn" would be seen as very offensive (not to me) to some members of this forum.

2) When you have an issue with a particular concept, the best way to approaching the solution is to attack the issue in the most basic way i.e. write the minimum amount of code to either solve your problem or at least demonstrate what the problem is. When you're trying to solve an issue with a new concept buried amongst all the other "noise" being the other code in your application it is very easy to get misguided in to what the actual problem is. This is nothing new. Here's a link to a "sticky" post at the head of the C forums by Narue - she is more articulate in her explanation of this. In addition, she provides very useful guidelines on how or why you should post messages to a forum.

http://www.daniweb.com/forums/thread213874.html

Just as it is frustrating for you learning the CS game, it is equally as frustrating for us members with more knowledge and experience to help other members out when they post heaps of code and then say "help me". Now, I know you're not a poster of that nature - so please take some of this advice in to account when posting in the future - and for what it's worth I'll help out if or when I can.

Regards,
JD

>So if you did mean well.. and even if it came out in a bent, skewed
>and poorly communicated way, well thank you I do actually appreciate that.

Too little, too late, and wording it like your misunderstanding is my fault. It's a wonder you have any friends at all. By the way, if you were truly interested in peace and happiness, you should have taken more time to understand my post before mouthing off.

You fail. Plonk.

Actually no I didn't fail.. over the weekend through more reading and trial and error I solved my issue myself.

I urge you to explore your reaction around this, and I don't mean replying to me in the thread or being defensive. For someone that was initially so blunt and seemingly cold, I am still being surprised by your high level of sensitivity and I think that is something you might want to explore.

I should have taken more time to understand? "No one cares about your feelings". I think unless you want to dump a whole heaps of hehehes and smiley faces in there, i read it how it was communicated which if it was all sunshine and love fits in the - poorly category. Communication is a two way street Narue and being involved in Comp Sci you should know how much of earthly communication is assumed.

Communication between humans is a fine and odd mix mix of cultural beliefs, tone, body language, facial expressions, volume, previous experiences eg your up-bringing, and so on. So when you are initiating communication you need to think also in the way in which a message can be interpreted, then go about finding the means to help transmit what you say, clearly. So again if that was love and sunshine that left your keyboard that day.. it sure and to hell was communicated in a very odd way.

So where do we go from here? You can remain all defensive, and make more comments like "no way buddy.. you looooose" or you can see that there is actually a lesson in this for all of us, me included.

Nate

Well I never expected such a reaction to my post #3.

@nateuni
First up, I apologise if that post offended you that much. In hindsight, I perhaps should have let the post sit in draft mode before pressing the ol' Submit button.

etc etc
JD

JD,

Thanks for taking the time to reply.

I agree that my questions could be more clear and refined. And instead of just posting y code perhaps some example code would have been better. I have also been tackling this issue with Uni.. as if I send a question and it is not understood.. I have to wait another 24 hours for a reply. So this is a lesson for me and corresponds part 2. of your reply.

In reply to number 1. Well it was about pointers which pointed to a 2d arrays, whether they were needed or not I believed upn the time of writing that this was my issue.

I reply to offending people with goddamn.. if people are that sensitive to my own frustration around pointer and are find that my comment of pointers (in this case in my program) were damned by god, I suggest that they don't turn on the TV or go outside.. because I think that is mild compared to what is on offer in the world.

I have solved the issue now, I found a book that seemed to explain the situation in a way that gelled with me. I then proceeded to go write a basic program to work with pointers in functions and print out the hex address and also the value they were pointing too. Even though it is still a bit of a mind **** I am able to now make my code function correctly and is warning/error free.

The issue was that I was passing an array into a function as a reference (&) then dereferencing it (*), say like I would an int. When this doesn't happen this way. To add to the messup I was dealing with a 2d array which I have never used before that program. So I did not know that the size of the 2 dimension (aka column) needed to be clearly expressed in the receiving function. Then when I went about changing this, I still got errors.. but that was because the argument that stated the number of column needed to be passed in before the array.

So when I was faced with all these factors and not knowing what was the correct syntax.. it was bloody confusing and extremely frustrating, I came on here to see if someone could just give me some pointers on how I was not obeying the syntax. I was seeking some human guidance something which is few and far between as an external student. I spend most of the time figuring this stuff out alone.

Ok no hard feeling, thanks for taking the time to clarify it is appreciated, and PS I thought the iAte was a pretty good one.

Regards,
Nate

hahah narki gave me a negative rep point.. ahhaha..

I wish I could send her a virtual tissue box.. ahhaha. That made me laugh... what a looooser!

commented: This has gove well beyond a thread about code and seems to be about whining and being an ass. +27
commented: I think what Dave meant was... This has gove well beyond a thread about code and seems to be about whining and being an ass. -2
commented: that was definitely uncalled for. Perhaps you should take your own advice into consideration here +0

@OP : is the God dam a typecast to pointers, because you hate them?

commented: Very witty post .. brought a smile to my face. +6
commented: Partly fixed previous oops. -5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.