Thread: working with srings adresses and pointers,

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then compile with respect to C99 or later, e.g., pass -std=c99 as an option to gcc.
    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

  2. #32
    Registered User
    Join Date
    Apr 2014
    Posts
    26
    well the exercise asks to compile with those particular options i said on the last comment so i can't add any other option.

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Then you can use dynamic memory allocation with say, malloc and free.
    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

  4. #34
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Lired View Post
    so, if i understood correctly, this code:
    <snip>
    only doesn't work if the arrays are not of the same type? But since they are both char arrays then the pointers can be subtracted, right? So for strings this code is correct and it even works with your example (abcdefghij, it returns abcddefhij)
    No. Reread my post #16 and #18. It's broken.
    Quote Originally Posted by Lired View Post
    well the exercise asks to compile with those particular options i said on the last comment so i can't add any other option.
    Well, how about you point us to the complete assignment description, instead of giving us little tiny bits of it every 5-10 posts. The whole thing, so we know exactly what you are supposed to be doing. I suspect you aren't even really sure what the assignment is asking.

    If you don't understand the problem, then you can't solve it.
    If you can't solve it, then you can't program a computer to solve it.

  5. #35
    Registered User
    Join Date
    Apr 2014
    Posts
    26
    Quote Originally Posted by anduril462 View Post
    No. Reread my post #16 and #18. It's broken.
    Then please explain me where im thinking wrong thats what im asking. it works with every example you said so please explain a specific example where it doesnt, untill now i havent seen a single example where it doesnt.

    in post 16 and 18 you said it was broken because the pointers could point to diferent objects, so i ask if both arrays are strings then it works because they are of the same type.

    Also the second code i cant see where it's wrong, the source string can't be too big, but no word is that big to make it a problem so i think it is ok too.

    Well, how about you point us to the complete assignment description, instead of giving us little tiny bits of it every 5-10 posts. The whole thing, so we know exactly what you are supposed to be doing. I suspect you aren't even really sure what the assignment is asking.
    these are the facts:

    move n bytes from area m2 to area m1 (this function must work even if m1 and m2 share memory space).

    its about strings, thats a fact and the strings can't have spaces on them, thats another fact. I know this because it is on a chapter of exercises about words that come from a wordsearch.

    the exercise it's suposed to be compiled with
    gcc -ansi -Wall -pedantic -O2 and it must not give any warning, note, etc...

  6. #36
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Lired View Post
    Then please explain me where im thinking wrong thats what im asking. it works with every example you said so please explain a specific example where it doesnt, untill now i havent seen a single example where it doesnt.

    in post 16 and 18 you said it was broken because the pointers could point to diferent objects, so i ask if both arrays are strings then it works because they are of the same type.
    Same object and same type are not equivalent. For example, int x, y; declares two ints. They are both the same type, but are not the same actual object. Similarly
    Code:
    char foo[100];
    char bar[100];
    foo and bar are not the same object. If you pass them to your function and try to subtract them, it results in undefined behavior. Undefined behavior is bad. Read these two links:
    Question 11.33
    LLVM Project Blog: What Every C Programmer Should Know About Undefined Behavior #1/3
    Just because it seems to work doesn't mean it's a good idea. You're simply getting lucky.

    Quote Originally Posted by Lired View Post
    Also the second code i cant see where it's wrong, the source string can't be too big, but no word is that big to make it a problem so i think it is ok too.
    We're telling you that it is wrong. If you don't want to listen to us because it doesn't look wrong to you (even though you are a novice), then why even come here asking for help? And for the last bloody time, you need to test thoroughly. You should try to break your function. If you can't in any way break it, and it meets all the requirements, then you were successful. If you don't believe me that it's broken, try this:
    Code:
    char foo[10] = "short";
    char bar[40] = "supercalifragilisticexpialidocious";
    movefunction(foo, bar, 100);  // feel free to try a shorter number like 20
    printf("foo = %s\n", foo);
    printf("bar = %s\n", bar);
    Does the output look right to you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 06-01-2007, 07:02 PM
  2. Working with pointers...
    By CompiledMonkey in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 08:14 AM
  3. Ip adresses
    By Da-Nuka in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-27-2005, 02:25 PM
  4. msn adresses on XP
    By GanglyLamb in forum Tech Board
    Replies: 1
    Last Post: 07-10-2003, 05:27 AM
  5. Increamenting Array Adresses
    By Code-Disciple in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2003, 04:40 PM

Tags for this Thread