| | |
(God damn) POINTERS and passing into functions
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 51
Reputation:
Solved Threads: 0
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
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.
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
java Syntax (Toggle Plain Text)
#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.
Last edited by nateuni; Aug 29th, 2009 at 4:42 am. Reason: ops typo
>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/e...tion2.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-pro...-function.html
>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks.
Did you asked Google? Anyways, I did it for you: http://www-ee.eng.hawaii.edu/~dyun/e...tion2.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-pro...-function.html
>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks.
Siddhant Sanyam
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
•
•
>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/e...tion2.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-pro...-function.html
>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks.
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.
Last edited by yellowSnow; Aug 29th, 2009 at 11:41 am.
Manic twiddler of bits
•
•
Join Date: Aug 2009
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
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
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
•
•
Join Date: Aug 2009
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
>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/e...tion2.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-pro...-function.html
>3. Making comparisons between pointers and ints
Sorry I did not got this properly to make any remarks.
•
•
Join Date: Aug 2009
Posts: 51
Reputation:
Solved Threads: 0
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
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.
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.
New members chased away this month: 5
•
•
Join Date: Aug 2009
Posts: 51
Reputation:
Solved Threads: 0
•
•
•
•
>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
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
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 **** 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.
I was going to help you with your C question, but now I won't. Congratulations, you're alienating even your allies now.
Last edited by Narue; Aug 29th, 2009 at 4:02 pm.
New members chased away this month: 5
•
•
Join Date: Aug 2009
Posts: 51
Reputation:
Solved Threads: 0
When you learn some diplomacy then I would love you to help me with my C.
But with opening comments like - [I]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
But with opening comments like - [I]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
![]() |
Similar Threads
- C++ Performance Tips (C++)
- cin, cout, matrix, pointers and references :( (C++)
- Function pointers inside classes (C++)
- C++ Object Pointers Problem (C++)
- Passing a Function, function pointer (C++)
- How to see if one string is less than another string (C)
- AP Computer Science Exam (Java)
- Need Help counting Array Length (C++)
Other Threads in the C Forum
- Previous Thread: understanding 2d char arrays
- Next Thread: Conversion in C Language
Views: 1038 | Replies: 19
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation unix user variable visualstudio voidmain() wab win32 win32api windows.h






